Django NoReverseMatch错误与命名空间,模板渲染时出错

时间:2015-10-25 16:30:53

标签: python django views

我一整天都在看这个,我无法弄清楚这一点。

此时加载hotel / index.html时出现错误:

NoReverseMatch at /hotel/

Reverse for 'activities' with arguments '(2,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['hotel/activities/']

Request Method:     GET
Request URL:    http://127.0.0.1:8000/hotel/
Django Version:     1.8.5
Exception Type:     NoReverseMatch
Exception Value:    

Reverse for 'activities' with arguments '(2,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['hotel/activities/']

Exception Location:     /usr/local/lib/python3.4/dist-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 495
Python Executable:  /usr/bin/python3
Python Version:     3.4.3
Python Path:    

['/home/johan/sdp/gezelligehotelletjes_com',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/usr/lib/python3.4/lib-dynload',
 '/usr/local/lib/python3.4/dist-packages',
 '/usr/lib/python3/dist-packages']

Server time:    Sun, 25 Oct 2015 16:18:00 +0000
Error during template rendering

In template /home/johan/sdp/gezelligehotelletjes_com/hotel/templates/hotel/index.html, error at line 8
Reverse for 'activities' with arguments '(2,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['hotel/activities/']
1   {% load staticfiles %}
2   
3   <link rel="stylesheet" type="text/css" href="{% static 'hotel/style.css' %}" />
4   
5   {% if netherlands_city_list %}
6       <ul>
7           {% for city in netherlands_city_list %}
8   
                  <li><a href="
      {% url 'hotel:activities' city.id %}
      ">{{ city.name }}</a></ul>


9           {% endfor %}
10      </ul>
11  {% else %}
12      <p>No polls are available.</p>
13  {% endif %}

以下是我认为与此错误相关的代码。

网站/ urls.py

from django.conf.urls import include, url
from django.contrib import admin

import hotel.views

urlpatterns = [
    url(r'^hotel/', include('hotel.urls', namespace='hotel')),
    url(r'^admin/', include(admin.site.urls)),
]

酒店/ urls.py

from django.conf.urls import include, url
from django.contrib import admin

from hotel import views

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^activities/$', views.ActivitiesView.as_view(), name='activities'),
]

酒店/ index.html中

{% load staticfiles %}

<link rel="stylesheet" type="text/css" href="{% static 'hotel/style.css' %}" />

{% if netherlands_city_list %}
    <ul>
        {% for city in netherlands_city_list %}
            <li><a href="{% url 'hotel:activities' city.id %}">{{ city.name }}</a></ul>
        {% endfor %}
    </ul>
{% else %}
    <p>No cities are available.</p>
{% endif %}

酒店/ activities.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

</body>
</html>

和hotel / views.py

from django.shortcuts import render
from django.views import generic
from django.core.urlresolvers import reverse
from .models import Hotel
from base.models import City


class IndexView(generic.ListView):
    template_name = "hotel/index.html"
    context_object_name = "netherlands_city_list"

    def get_queryset(self):
        return City.objects.filter(state__country__name__exact="Netherlands").order_by('name')[:5]


class ActivitiesView(generic.ListView):
    template_name = "hotel/activities.html"

我现在整天都在四处寻找,但无法弄清楚这一点。 (虽然它可能是那些较小的东西之一。)

我希望有人可以帮助解决这个问题。

提前致谢。

2 个答案:

答案 0 :(得分:3)

您的问题出在您的网址中:

url(r'^activities/$', views.ActivitiesView.as_view(), name='activities'),

您的模板调用参数活动:

<a href="{% url 'hotel:activities' city.id %}">

但是这个论点并没有作为参数传递。解决方案是:

url(r'^activities/(?P<city>[0-9]+)/$', views.ActivitiesView.as_view(), name='activities'),

答案 1 :(得分:2)

在模板中,您尝试与位置参数(城市ID)进行反向匹配

{% url 'hotel:activities' city.id %}

但是,在您的urlpatterns中,您尚未在网址定义中设置任何参数规则

url(r'^activities/$', views.ActivitiesView.as_view(), name='activities'),

所以,你有这个错误

Reverse for 'activities' with arguments '(2,)' ... not found. 

检查文档以完成您的目标。

https://docs.djangoproject.com/en/1.8/topics/http/urls/

您可能需要添加城市ID与您的网址匹配的正则表达式组

url(r'^activities/([0-9]+)/$', views.ActivitiesView.as_view(), name='activities'),

(后者只是一个想法,你需要根据你的情况修改它)。