Amon
Amon

Reputation: 2971

404 error when viewing details of a poll in Django

I'm working through the Django tutorial anf. For some reason, if I try to manually type in the URL like so: http://localhost:8000/polls/1/ I can view the poll (in this case the id of the poll is 1). But if I'm at the index of all the polls, which would be the URL: http://localhost:8000/polls and I try to click on one of the polls, it takes me to an error page with the following:

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.

Here's my polls/urls file:

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

And my mysite/urls file:

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

And my template file:

    {% 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 %}

I've seen similar questions but it doesn't seem that they apply to me. Why does it work when I type in the URL but not when I click on the link that should take me to the same place?

Upvotes: 0

Views: 344

Answers (1)

sajadkk
sajadkk

Reputation: 764

You have space between % and }, try this

<a href="{% url 'polls:detail' poll.id %}">

Upvotes: 3

Related Questions