supersheep1
supersheep1

Reputation: 721

django error NoReverseMatch while i rendering

I'm creating my first django app using this tutorial https://docs.djangoproject.com/en/dev/intro/tutorial04/ .I'm up to the final task and now I have this error which I don't know how to fix.I'm going to show you my whole program and I'll try to pin point at where I think I went wrong in making this poll.This app is like Poll. It display a poll and some choice and you must vote on it.

This is my error

TemplateSyntaxError at /polls/1/
Caught NoReverseMatch while rendering: u'myapp' is not a registered namespaceRequest Method: GET 
Request URL: http://cat.pythonanywhere.com/polls/1/ 
Django Version: 1.3.5 
Exception Type: TemplateSyntaxError 
Exception Value: Caught NoReverseMatch while rendering: u'myapp' is not a registered namespace 
Exception Location: /usr/local/lib/python2.7/site-packages/django/template/defaulttags.py in render, line 450 
Python Executable: /usr/local/bin/uwsgi 

Template error
In template /home/cat/mysite/myapp/templates/myapp/detail.html, error at line 5

Caught NoReverseMatch while rendering: u'myapp' is not a registered namespace
1 <h1>{{ poll.question }}</h1>

2  
3 {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

4  
5 <form action="{% url myapp:vote poll.id %}" method="post">

6 {% csrf_token %}

7 {% for choice in poll.choice_set.all %}

8     <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />

9     <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />

10 {% endfor %}

11 <input type="submit" value="Vote" />

12 </form> 

I think the error is hiding in my detail.html

<h1>{{ poll.question }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url myapp:vote poll.id %}" method="post">
{% csrf_token %}
{% for choice in poll.choice_set.all %}
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>

My Urls.py

from django.conf.urls.defaults import *
from mysite.myapp import views

urlpatterns = patterns('',

    url(r'^$', views.index, name='index'),
    url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
    url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
    url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)

I hope someone can help me because I have no idea how to fix this error

Upvotes: 0

Views: 1407

Answers (3)

Kevin Ogoro
Kevin Ogoro

Reputation: 1127

If your main urls.py contains

url (r'^poll/', include('poll.urls'))

then change to

url (r'^poll/', include('poll.urls', namespace='poll'))

This worked for me.

Upvotes: 0

catherine
catherine

Reputation: 22808

In your main url where your admin url save, your main urlconf poll must be like this to registered that namespace:

main urls.py

 url (r'^poll/', include('poll.urls', namespace='poll')),

then in the child urls.py

urlpatterns = patterns('poll.views',
    url(r'^$', index, name='index'),
    url(r'^(?P<poll_id>\d+)/$', 'detail', name='detail'),
    url(r'^(?P<poll_id>\d+)/results/$', 'results', name='results'),
    url(r'^(?P<poll_id>\d+)/vote/$', 'vote', name='vote'),
)

Upvotes: 2

dshap
dshap

Reputation: 1402

Try changing {% url myapp:vote poll.id %} to {% url vote poll.id %} in your template

Upvotes: 0

Related Questions