Pergunta

I am following the Django tutorial and have made it to Decoupling the URLConfs in tutorial 3. Prior to this step, everything was working. Now, when I do the final step of Removing hardcoded URLs in templates which is changing

<li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>

to

<li><a href="{% url 'polls.views.detail' poll.id %}">{{ poll.question }}</a></li>

I get this error:

NoReverseMatch at /polls/

Reverse for ''polls.views.detail'' with arguments '(1,)' and keyword arguments '{}' not found.

Request Method:     GET
Request URL:    http://localhost:8000/polls/
Django Version:     1.4
Exception Type:     NoReverseMatch
Exception Value:    

Reverse for ''polls.views.detail'' with arguments '(1,)' and keyword arguments '{}' not found.

Exception Location:     e:\Django\development\tools\PortablePython\PortablePython2.7.3.1\App\lib\site-packages\django\template\defaulttags.py in render, line 424
Python Executable:  e:\Django\development\tools\PortablePython\PortablePython2.7.3.1\App\python.exe

My views.py looks like this:

from django.shortcuts import render_to_response, get_object_or_404
from polls.models import Poll

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

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

def results(request, poll_id):
    return HttpResponse("You're looking at the results of poll %s." % poll_id)

def vote(request, poll_id):
    return HttpResponse("You're voting on poll %s." % poll_id)

My project urls.py looks like this:

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

from django.contrib import admin
admin.autodiscover()

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

And polls/urls.py looks like this:

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

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

Obviously I missed something, but I have been over part 3 a few times now and can't figure out what I missed. What do I need to correct to correctly decouple these URLs?

Foi útil?

Solução

This is a version issue. You've somehow found a link for the development version of Django, while you're using release 1.4. One of the things that's changed since the release is that URL names in templates used not to need quotes, but now they do. That's why the error message has the URL name within two sets of quotes.

You should use this version of the tutorial to match the Django version you have. (You could install the development version, but that's not recommended - stick to the release.)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top