Johan Vergeer
Johan Vergeer

Reputation: 5568

Django NoReverseMatch error with namespacing, Error during template rendering

I have been looking at this all day now and I am not able to figure this out.

When loading hotel/index.html at this moment I get an error:

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

Here is the code that I think relates to this error.

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>

and 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"

I have been looking around all day now but can't figure this out. (While it is probably one of those smaller things.)

I hope someone can help with this issue.

Thanks in advance.

Upvotes: 0

Views: 587

Answers (2)

Puam Dias
Puam Dias

Reputation: 246

Your problem is in your URLs:

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

Your template calls the arguments activities:

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

But this argument isn't passed as a parameter. The solution is:

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

Upvotes: 3

anti1869
anti1869

Reputation: 1409

In your template you try to get reverse match with positional argument (city id)

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

However, in your urlpatterns, you have not set up any argument rules in url definition

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

So, you have this error

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

Check with the docs to accomplish what you are intended to do.

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

Probably you need to add regex groups with city id matching to your urls

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

(latter is just an idea, you need to modify it up to your case).

Upvotes: 2

Related Questions