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才能正確解耦?
謝謝。我甚至沒有注意到URL中的'dev'。 – Andy 2012-07-31 20:52:01