我在用django 1.10。我想知道反向函数是如何工作的。这是一个使用django的基本聊天应用程序。我被困在index.html的编号:7行,下面的文件是:

viwes.py
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from chat.models import ChatRoom
def index(request):
chat_rooms= ChatRoom.objects.order_by('name')[:5]
context = {
'chat_list' : chat_rooms
}
return render(request,'chats/index.html', context)
def chat_room(request,chat_room_id):
chat = get_object_or_404(ChatRoom, pk =chat_room_id)
return render(request,'chats/chat_room.html', {'chat':chat})聊天/urls.py
from django.conf.urls import url
from django.urls import reverse
from chat import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<chat_id>[0-9]+)/$', views.chat_room, name='chat_room'),
]index.html
{% if chat_list %}</pre>
<ul>
<ul>{% for chat in chat_list %}</ul>
</ul>
<ul>
<ul>
<li><a id="" href="{% url 'chat_room' chat.id %}"> {{ chat.name }}</a></li>
</ul>
</ul>
<ul>{% endfor %}</ul>
<pre>
{% else %}
No chats are available.
{% endif %}欢迎任何建议!
发布于 2017-09-13 13:44:02
正如我在您的重新编码中所看到的,您有一个错误:
{% url 'chat_room' chat_room_id %}
<!-- ^^^^^^^^^^ -->但在问题中你写的是正确的
{% url 'chat_room' chat.id %}只需将其应用于您的代码。
https://stackoverflow.com/questions/46198094
复制相似问题