2012-07-31 44 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