I have a Django project with this layout
在baseApp www.example.com
def Home(request):
rooms = Room.objects.all()
context = {'rooms':rooms}
return render(request,'base/home.html',context)
def room(request,pk):
room = Room.objects.get(id=pk)
context = {'room':room}
return render(request,'base/room.html',context)
def createRoom(request):
context= {}
return render(request,'base/room_form.html',context)
在baseApp www.example.com
urlpatterns = [
path('', views.Home, name="home page"),
path('<pk>/', views.room, name="room"),
path('create-room/',views.createRoom, name="create-room"),
]
在基地/临时/家里。超文本标记语言
<h2> <a href="{% url 'create-room' %}">Create Room</a></h2>
以room_form的形式。超文本标记语言
{% extends 'main.html' %}
{% block content %}
<div>
<form method="=Post" action="">
{% csrf_token %}
<input type="submit" value="Submit" />
</form>
</div>
{% endblock content%}
它应该转到这个URL: www.example.com ”到room_form。html”,但它转到: www.example.com with error Field 'id' expected a number but got 'create-room'
traceback (most recent call last):
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 2055, in get_prep_value
return int(value)
The above exception (invalid literal for int() with base 10: 'create-room') was the direct cause of the following exception:
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/kotp/Desktop/python/P_project/chatroom/base/views.py", line 23, in room
room = Room.objects.get(id=pk)
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/query.py", line 623, in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/query.py", line 1436, in filter
return self._filter_or_exclude(False, args, kwargs)
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/query.py", line 1454, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/query.py", line 1461, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1498, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1529, in _add_q
child_clause, needed_inner = self.build_filter(
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1444, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1271, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/lookups.py", line 27, in __init__
self.rhs = self.get_prep_lookup()
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/lookups.py", line 341, in get_prep_lookup
return super().get_prep_lookup()
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/lookups.py", line 85, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "/home/kotp/Desktop/python/P_project/pvenv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 2057, in get_prep_value
raise e.__class__(
Exception Type: ValueError at /create-room/
Exception Value: Field 'id' expected a number but got 'create-room'.
我期待它应该转到这个URL: www.example.com 。超文本标记语言
1条答案
按热度按时间kqhtkvqz1#
它不会“触发”
createRoom
,你没有指定pk
的类型,所以它需要**<str:…>
[Django-doc],这意味着create-room/
将需要pk='create-room'
。您可以指定类型为
<int:…>
**[Django-doc],因此: