2017-09-13 3 views
1

django 1.10을 사용하고 있습니다. 리버스 함수가 ​​어떻게 작동하는지 알고 싶습니다. 이것은 장고를 사용하는 기본 채팅 응용 프로그램입니다. 파일이있는 7 것들 아래 index.html에 : 나는 더 라인에 붙어하지 오전NoReverseMatch at/<urls>

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'), 
] 

인덱스를. 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 %} 

모든 제안을 환영합니다! 내가 당신의 reeal 코드에서 볼 수 있듯이

+1

추가하십시오

{% url 'chat_room' chat.id %} 

은 당신의 코드에 적용 올바른 쓰기 전체 오류 스택 –

+0

당신은'url'을 시도 했습니까? 이것을'localhost'에서 실행하면'127.0.0.1 : 8000'이 당신의 색인이어야하고'127.0.0.1 : 8000/id'이 당신의 대화방이되어야합니다. 'id'는 임의의 숫자 여야합니다. 다른'url'은 정의되어 있지 않습니다. 올바른 'url'을 입력했는지 확인하십시오. –

+0

127.0.0.1:8000/chats/ –

답변

0

당신은 오타가 :

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

을하지만, 질문에 당신은

+0

고맙습니다! 그것은 일하고있다! –

+0

chat.id에 대해 왜 말해 줄 수 있습니까? –

+0

당신의 회 돌이'chat'에 모델'ChatRoom'의 인스턴스이므로'id'를 사용할 필요가 있습니다 –