NoReverseMatch位于/ <urls>

时间:2017-09-13 12:50:23

标签: python django django-templates

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

enter image description here

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 %}

欢迎任何建议!

1 个答案:

答案 0 :(得分:0)

正如我在您的reeal代码中看到的那样,您有一个拼写错误:

 {% url 'chat_room' chat_room_id %}
                  <!-- ^^^^^^^^^^ -->

但是你写的问题是正确的

{% url 'chat_room' chat.id %}

只需将其应用于您的代码

即可
相关问题