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

Django NoReverseMatch error with namespacing, Error during template rendering

我一整天都在看这个,但我无法弄明白。

此时加载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 %}

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

site/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)),
]

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

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

hotels/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"

我一整天都在四处张望,但还是想不通。 (虽然它可能是那些较小的东西之一。)

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

提前致谢。

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

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

但是,在您的url模式中,您没有在url定义

中设置任何参数规则
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 匹配添加到您的 urls

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

(后者只是一个想法,您需要根据自己的情况进行修改)。

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

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