我正在研究Django教程anf。出于某种原因,如果我尝试手动输入URL,如下所示:http://localhost:8000/polls/1/
我可以查看轮询(在这种情况下,轮询的id为1)。但是,如果我在所有民意调查的索引处,即URL http://localhost:8000/polls
,并且我尝试点击其中一个民意调查,则会转到错误页面,其中包含以下内容:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/ ^$ [name='index']
^polls/ ^(?P<pk>\d+)/$ [name='detail']
^polls/ ^(?P<pk>\d+)/results/$ [name='results']
^polls/ ^(?P<poll_id>\d+)/vote/$ [name='vote']
^admin/
The current URL, polls/{% url 'polls:detail' poll.id % }, didn't match any of these.
这是我的polls/urls
文件:
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
我的mysite / urls文件:
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace='polls')),
url(r'^admin/', include(admin.site.urls)),
)
我的模板文件:
{% 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>No polls are available.</p>
{% endif %}
我见过类似的问题,但似乎并不适用于我。当我输入网址时为什么它可以工作,但是当我在链接上点击时却不能将我带到同一个地方?
答案 0 :(得分:3)
%和}之间有空格,试试这个
<a href="{% url 'polls:detail' poll.id %}">