2012-07-31 43 views
0

我正在关注Django教程,并且已经在教程3中使用了Decoupling the URLConfs。在此步骤之前,一切正常。现在,当我删除模板硬编码URL的最后一步,它正在改变如何正确分离Django教程3中的URLConf?

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

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

我得到这个错误:

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 

views.py看起来是这样的:

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) 

我的项目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')), 
    url(r'^admin/', include(admin.site.urls)), 
) 

而且polls/urls.py看起来是这样的:

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'), 
) 

显然我错过了什么,但我已经结束了第3部分几遍想不通我错过了什么。我需要纠正哪些URL才能正确解耦?

回答

4

这是一个版本问题。在您使用1.4版时,您已经以某种方式找到了Django开发版的链接。自发布以来,其中一件事情发生了变化,那就是模板中的URL名称不需要引号,但现在它们可以使用。这就是为什么错误消息具有两组引号内的URL名称的原因。

您应该使用this version of the tutorial来匹配您拥有的Django版本。 (您可以安装开发版本,但不建议 - 坚持发布。)

+0

谢谢。我甚至没有注意到URL中的'dev'。 – Andy 2012-07-31 20:52:01