Django - How to get a form template to run (getting a "namespace without errors")

I am new to Django. Got Django 1.4.2 works with Python 2.7 on Windows 7.

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

So far, here is 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 urls.py project:

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 respond to a request like cathy, here is the voting 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 URL of the project folder if this can 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')),

)

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

!

+5
3
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 %}
+5

settings.py . , .

:

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',

)

, .

0

Try putting it {% load url from future %}at the top of your html file. This should solve your problem.

0
source

All Articles