Django Error u"'polls" is not a registered namespace

DjangoNamespaces

Django Problem Overview


Yesterday I was working on my first app using this tutorial. It's a Poll and Choice app. The first page displays the question and when you click on the question it's suppose to display choices which you can vote on them.

I had great people who helped me yesterday and told me to use namespace. I've read the namespace tutorial and tried to apply my knowledge to the scenario but it isn't working so far.

This is my error when I click on the questions which is the first page.

 NoReverseMatch at /polls/5/

 u"'polls" is not a registered namespace

 Request Method: 	GET
 Request URL: 	http://127.0.0.1:8000/polls/5/
 Django Version: 	1.4.3
 Exception Type: 	NoReverseMatch
 Exception Value: 	

 u"'polls" is not a registered namespace

 Exception Location: 	C:\hp\bin\Python\Lib\site-packages\django\template\defaulttags.py in render, line 424
 Python Executable: 	C:\hp\bin\Python\python.exe
 Python Version: 	2.5.2
 Python Path: 	

 ['C:\\djcode\\mysite',
  'C:\\hp\\bin\\Python\\python25.zip',
  'C:\\hp\\bin\\Python\\DLLs',
  'C:\\hp\\bin\\Python\\lib',
  'C:\\hp\\bin\\Python\\lib\\plat-win',
  'C:\\hp\\bin\\Python\\lib\\lib-tk',
  'C:\\hp\\bin\\Python',
  'C:\\hp\\bin\\Python\\lib\\site-packages',
  'C:\\hp\\bin\\Python\\lib\\site-packages\\win32',
  'C:\\hp\\bin\\Python\\lib\\site-packages\\win32\\lib',
  'C:\\hp\\bin\\Python\\lib\\site-packages\\Pythonwin']

 Server time: 	Fri, 15 Feb 2013 21:04:10 +1100
 Error during template rendering

 In template C:\djcode\mysite\myapp\templates\myapp\detail.html, error at line 5
 u"'polls" is not a registered namespace
 1 	<h1>{{ poll.question }}</h1>
 2 	
 3 	{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
 4 	
 5 	{% url 'polls:vote' poll.id %}
 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>

Now I know the problems are hidden in detail.html, my main urls and my app called myapp URLCONF and views.py

Now My main URLconf are: C:\djcode\mysite\mysite

 from django.conf.urls import patterns, include, url
 from django.contrib import admin
 from django.conf import settings
 # Uncomment the next two lines to enable the admin:
 # from django.contrib import admin
 admin.autodiscover()

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

My app folder is called myapp and this is myapp URLconf: C:\djcode\mysite\myapp

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

 from django.conf.urls import patterns, include, url
 
 urlpatterns = patterns('myapp.views',
     url(r'^$', 'index'),
     url(r'^(?P<poll_id>\d+)/$', 'detail'),
     url(r'^(?P<poll_id>\d+)/results/$', 'results'),
     url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
 

)
 

My views.py inside myapp are:

 from django.http import HttpResponse
 from myapp.models import Poll ,choice
 from django.template import Context, loader
 from django.http import Http404
 from django.shortcuts import render_to_response, get_object_or_404
 from django.template import RequestContext

 def index(request):
     latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
     return render_to_response('myapp/index.html', {'latest_poll_list': latest_poll_list})

 def results(request, poll_id):
     p = get_object_or_404(Poll, pk=poll_id)
     return render_to_response('myapp/results.html', {'poll': p})

 def vote(request, poll_id):
     p = get_object_or_404(Poll, pk=poll_id)
     try:
         selected_choice = p.choice_set.get(pk=request.POST['choice'])
     except (KeyError, Choice.DoesNotExist):
         # Redisplay the poll voting form.
         return render_to_response('myapp/detail.html', {
             'poll': p,
             'error_message': "You didn't select a choice.",
         }, context_instance=RequestContext(request))
     else:
         selected_choice.votes += 1
         selected_choice.save()
         # Always return an HttpResponseRedirect after successfully dealing
    # with POST data. This prevents data from being posted twice if a
         # user hits the Back button.
         return HttpResponseRedirect(reverse('myapp.views.results', args=(p.id,)))

 def detail(request, poll_id):
     p = get_object_or_404(Poll, pk=poll_id)
     return render_to_response('myapp/detail.html', {'poll': p},
                                context_instance=RequestContext(request))

My detail.html C:\djcode\mysite\myapp\templates\myapp

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

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

 {% url 'polls:vote' poll.id %}
 {% 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>

Django Solutions


Solution 1 - Django

The answer is to add namespaces to your root URLconf. In the mysite/urls.py file (the project’s urls.py, not the application’s), go ahead and change it to include namespacing:

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

Furthermore, in part 3 of the tutorial Namespacing URL names the use of app_name is mentioned as the accepted way for adding the polls namespace. You can add the line for this in your polls/urls.py as follows:

app_name = 'polls'
urlpatterns = [
    ...
]

Solution 2 - Django

Following the same Django tutorial and having the same names, I had to change in mysite/urls.py from:

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

to:

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

Solution 3 - Django

You need to add the following line to the top of the detail.html:

{% load url from future %}

(Notice you've already used this line in the index.html in order to use the polls namespace)

Solution 4 - Django

Django 2.0

in yourapp/urls.py

from django.urls import path
from . import views

app_name = 'yourapp'

urlpatterns = [
	path('homepage/', views.HomepageView.as_view(), name='homepage'),
]

in urls.py

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('yourapp/', include('yourapp.urls')),
    ]

Solution 5 - Django

Inside myapp/urls.py add the following module-level attribute:

app_name = "polls"

This will set the "application namespace name" for that application. When you use names like "polls:submit" in a reverse, Django will look in two places: application namespaces (set like above), and instance namespaces (set using the namespace= parameter in the "url" function). The latter is important if you have multiple instances of an app for your project, but generally it's the former you want.

I had this very issue, and setting namespace= into the url() function seemed wrong somehow.

See this entry of the tutorial: https://docs.djangoproject.com/en/1.9/intro/tutorial03/#namespacing-url-names

Update: this information is correct for Django 1.9. Prior to 1.9, adding a namespace= attribute to the include is, indeed, the proper way.

Solution 6 - Django

I think you missed the namespace:

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

Solution 7 - Django

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

 

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

----------------------------------    

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

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

 <form method="post" action="{% url myapp:vote poll.id %}">
 {% 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>

Solution 8 - Django

Replacing the line: {% url 'polls:vote' poll.id %} with: {% url 'vote' poll.id %} worked out for me...

Solution 9 - Django

I also faced the same issue. it is fixed now by adding app_name = "<name of your app>" in app/urls.py

Solution 10 - Django

namespace should be added polls/urls.py file.

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

Solution 11 - Django

For anyone using "django-hosts":

I had the same error and for me adding this to my template solved it (without any us of namespace etc.):

{% load hosts %}
<a href="{% host_url 'YOUR_URL' host 'YOUR_HOST' %}">Admin dashboard</a>

Additionaly I added PARENT_HOST = 'YOUR_PARENT_HOST' to my settings.py

Reference

Solution 12 - Django

The problem is in the tutorial. While adding the namespace( in your case 'myapp') to your URLconf, the tutorial uses the following code of line:

app_name = 'myapp'

The Django framework for some reason treats it as a unicode string. Instead please enclose the name of your app in double quotes instead of single quotes. For example,

app_name = "myapp"

This will most certainly solve your problem. I had the same problem and doing so solved it.

Solution 13 - Django

Restart the web server. Just that.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsupersheep1View Question on Stackoverflow
Solution 1 - DjangoJChen___View Answer on Stackoverflow
Solution 2 - DjangopartizanosView Answer on Stackoverflow
Solution 3 - DjangoIlialukView Answer on Stackoverflow
Solution 4 - DjangomontxeView Answer on Stackoverflow
Solution 5 - DjangoChris CogdonView Answer on Stackoverflow
Solution 6 - DjangoraghuView Answer on Stackoverflow
Solution 7 - DjangocatherineView Answer on Stackoverflow
Solution 8 - DjangoVaraView Answer on Stackoverflow
Solution 9 - Djangomuhammed fairoos nmView Answer on Stackoverflow
Solution 10 - DjangosanjayView Answer on Stackoverflow
Solution 11 - DjangonilsdetView Answer on Stackoverflow
Solution 12 - DjangoSumit ButolaView Answer on Stackoverflow
Solution 13 - DjangotherealsixView Answer on Stackoverflow