Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - How to get a form template to run (getting ' namespace not registered error ')

I am a newbie at Django. Got Django 1.4.2 working with Python 2.7 on Windows 7.

I am following a tutorial and I need to get a form template appear with radio buttons. Selecting a button sends data and the page should ask you if you want to 'vote again?'. Also, if you do not select a button and submit the form it should show a message asking you to make a choice.

So far, here's the HTML form:

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

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

<form action="{% url 'polls: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>

Now when I type http://localhost:8000/polls/1/ I should get the form, but instead I get the following error message:

NoReverseMatch at /polls/1/
u"'polls" is not a registered namespace

I registered polls as a namespace, see below in the project urls.py:

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

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),
)

To answer cathy's kind request, here's the vote url:

from django.conf.urls import patterns, url

from polls import views

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

.. and below I include the project folder's urls if this could help:

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

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

)

And I just noticed the following error message shown on the browser (because Debug = True):

Error during template rendering

In template C:\Python27\Scripts\mysite\mytemplates\polls\index.html, error at line 4

u"'polls" is not a registered namespace
    {% if latest_poll_list %}
        <ul>
        {% for poll in latest_poll_list %}
            <li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
        {% endfor %}
        </ul>
    {% else %}
        <p>No polls are available.</p>
    {% endif %} 

Any help to make that form run properly will be appreciated!

like image 841
Sylvain Avatar asked Feb 09 '13 21:02

Sylvain


People also ask

What does form {% URL %} do?

{% url 'contact-form' %} is a way to add a link to another one of your pages in the template. url tells the template to look in the URLs.py file. The thing in the quotes to the right, in this case contact-form , tells the template to look for something with name=contact-form .

What is form as_ p in django?

{{ form.as_p }} – Render Django Forms as paragraph. {{ form.as_ul }} – Render Django Forms as list.

What method can you use to check if form data has changed when using a form instance?

Use the has_changed() method on your Form when you need to check if the form data has been changed from the initial data. has_changed() will be True if the data from request.

What is URL namespace in Django?

URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It's a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed.


1 Answers

urlpatterns = patterns('polls.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'),
)

{% url polls:detail poll.id %}
like image 90
catherine Avatar answered Sep 29 '22 07:09

catherine