I'm learning Django from [THIS][1] tutorial. Everything worked perfectly until this step:
Now change your polls/index.html template from:
<li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li>
to point at the namespaced detail view:
<li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
You can see the whole paragraph [HERE][2].
And that's what happened after I changed 'detail' into 'polls:detail':
NoReverseMatch at /polls/
'polls' is not a registered namespace
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 1.6.4
Exception Type: NoReverseMatch
Exception Value:
'polls' is not a registered namespace
Exception Location: D:\Programy\Python 3.4\lib\site-packages\django\core\urlresolvers.py in reverse, line 528
Python Executable: D:\Programy\Python 3.4\python.exe
Python Version: 3.4.0
Python Path:
['D:\\dziango\\strona1',
'D:\\Programy\\Python 3.4\\python34.zip',
'D:\\Programy\\Python 3.4\\DLLs',
'D:\\Programy\\Python 3.4\\lib',
'D:\\Programy\\Python 3.4',
'D:\\Programy\\Python 3.4\\lib\\site-packages']
Server time: Wed, 21 May 2014 16:36:13 +0200
and here is my Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 1.6.4
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Template error:
In template D:\dziango\strona1\polls\templates\polls\index.html, error at line 1
'polls' is not a registered namespace
1 : {% 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>Nie ma ankiet.</p>
{% endif %}
Traceback:
File "D:\Programy\Python 3.4\lib\site-packages\django\core\handlers\base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\dziango\strona1\polls\views.py" in index
11. return render(request, 'polls/index.html', context)
File "D:\Programy\Python 3.4\lib\site-packages\django\shortcuts\__init__.py" in render
53. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "D:\Programy\Python 3.4\lib\site-packages\django\template\loader.py" in render_to_string
169. return t.render(context_instance)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\base.py" in render
140. return self._render(context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\base.py" in _render
134. return self.nodelist.render(context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\base.py" in render
840. bit = self.render_node(node, context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\debug.py" in render_node
78. return node.render(context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\defaulttags.py" in render
305. return nodelist.render(context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\base.py" in render
840. bit = self.render_node(node, context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\debug.py" in render_node
78. return node.render(context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\defaulttags.py" in render
196. nodelist.append(node.render(context))
File "D:\Programy\Python 3.4\lib\site-packages\django\template\defaulttags.py" in render
447. six.reraise(*exc_info)
File "D:\Programy\Python 3.4\lib\site-packages\django\utils\six.py" in reraise
550. raise value
File "D:\Programy\Python 3.4\lib\site-packages\django\template\defaulttags.py" in render
433. url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "D:\Programy\Python 3.4\lib\site-packages\django\core\urlresolvers.py" in reverse
528. key)
Exception Type: NoReverseMatch at /polls/
Exception Value: 'polls' is not a registered namespace
I tried every single solution from [THIS][3] similar topic but none of them works for me. I don't paste my code here because I wrote it all from that tutorial and it's exactly the same. Everything I did was done step by step according to it. But if you need my code anyway, please tell and I'll put it here.
Sorry for my English.
== EDIT == here is my root urls.py file:
from django.conf.urls import patterns, url, include
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
)
and this is polls/views.py:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from polls.models import Choice, Poll
# Create your views here.
def index(request):
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
context = {'latest_poll_list': latest_poll_list }
return render(request, 'polls/index.html', context)
def detail(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id)
return render(request, 'polls/detail.html', {'poll':poll})
def results(request, poll_id):
return HttpResponse("Wyniki ankiety %s!" %poll_id)
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(request, 'polls/detail.html', {
'poll':p,
'error_message':"Nie wybrałeś żadnej opcji.",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponse(reverse('polls:results', args=(p.id,)))
polls/urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
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'),
)
UPDATE: Okay, I've made a foolish mistake by placing my urls.py in the wrong directory.. sorry
Yes, namespace issue for me as well - the above response helped me!
Really important to make sure you are in /polls not /admin/polls when you are inputting your server link on the browser.
Also, this works in path as well.
For mysite/polls/urls - please add the below line of code
app_name = 'polls'
and then also your mysite/mysite/urls should look like below:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls', namespace="polls")),
path('admin/', admin.site.urls),
]
you should add app name as like below in polls/urls.py
, I had the same issue but solved it by adding this:
app_name = 'polls'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With