21

I'm following the official tutorial to learn Django and using 1.5.

I had this link as part of my index template, which was working fine:

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

however, this is hardcoded and the tutorial suggested a better way was to use:

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

so that you'll be better of when dealing with huge number of templates and u have to make changes to the url.

Since I made the above change I get the following errors when I run the app:

Exception Type: NoReverseMatch
Exception Value:    Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found.

My urls.py looks like this:

from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
    url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
   url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),                     
)

views.py looks like this:

from django.shortcuts import render, get_object_or_404
from django.http import Http404

from polls.models import Poll

def index(request):
    latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
    context = {'latest_poll_list': latest_poll_list}
    return render(request, 'polls/index.html', context)


def detail(request, poll_id):
    poll = get_object_or_404(Poll, pk = poll_id)
    return render(request, 'polls/detail.html', {'poll': poll})

my index.html template looks like this:

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

Usually I could easily read where the error is coming from and deal with it but in this case I can't spot the cause of the error hence I'm unable to progress with my study. Any help will be greatly appreciated.

dmvianna
  • 15,088
  • 18
  • 77
  • 106
  • 1
    One thing I noticed is that you use namespace notation (`polls:detail`) in `index.html`, but don't define `polls` as a namespace in your `urlpatterns`. Try make it just `detail` without `polls:` for a moment. And shouldn't `poll_id` be `poll.id` there? – Thijs van Dien Oct 12 '13 at 15:48
  • thanks very much.. The problem was with poll_id instead of poll.id. it works now. Can you put it up as the answer so i can accept it? thanks again – fromPythonImportNoob Oct 12 '13 at 16:03

8 Answers8

29

In your index.html you gave poll_id as an argument, but that's just the name the argument will have within the detail function; it is not defined in your template. The actual value you want to call the function with is probably poll.id.

Thijs van Dien
  • 6,516
  • 1
  • 29
  • 48
13

My mistake was a typo on detail.html:

<form action={% url 'polls:vote' polls.id %}" method="post">

should have been

<form action={% url 'polls:vote' poll.id %}" method="post">

It took a while for me to realise the django traceback page was pointing me to the relevant line of code the whole time. :$

dmvianna
  • 15,088
  • 18
  • 77
  • 106
1

This happened to me when I was reading tutorial. I didn't change poll_id to pk:

url(r'^(?P<poll_id>\d+)/$', views.DetailView.as_view(), name='detail'),

vs

url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
Vanuan
  • 31,770
  • 10
  • 98
  • 102
1

I encountered this error when I was using a string as a raw value, rather than surrounding by quotes i.e.

{% url 'my_view' string_val %}

instead of

{% url 'my_view' 'string_val' %}

Zach Nussbaum
  • 11
  • 1
  • 4
0

I struggled with this for a while. Then I noticed I had put poll.id and not Poll.id with a (capital P)

sam
  • 355
  • 5
  • 10
0

also, in

polls/urls.py

i had spelling error

url(r'^(?P[0-9]+)/$', views.detail, name='details'),

vs the correct code

url(r'^(?P[0-9]+)/$', views.detail, name='detail'),

spent some time looking for the error, so look for proper spelling. lol

0

The error got sorted out for me after correcting the filter condition in views.py.

snippet of my views.py

def post_share(request, post_id):
        post = get_object_or_404(Post, id=post_id, status='Published')

snippet from my models.py

class Post(models.Model):
STATUS_CHOICES=(
                ('draft','Draft'),
                ('published','Published'),
                )

1st value is stored in the database and the second value is for displaying to the users.

raw data from my mysql DB

+---------------------------------------+-----------+
| title                                 | status    |
+---------------------------------------+-----------+
| Revolution 2020                       | published |
| harry potter and the sorcerer's stone | published |
| harry potter and the cursed child     | draft     |
| five point someone                    | published |
| half girlfriend                       | draft     |
| one night at the call center          | published |
| Django by example                     | published |
+---------------------------------------+-----------+

When I had used "published", I was getting the said error. Once I changed the filter to "Published" it all sorted out.

Sharath K P
  • 41
  • 1
  • 4
0

Be careful with your primary key datatype. In my case, i mistakently used int instead str.

if pk is string,

 path('addesm/pending/<str:pk>', views.addesm, name='add ESM')
Pravin L
  • 31
  • 1
  • 5