2
votes

I'm learning Django from djangoproject https://docs.djangoproject.com/en/1.5/intro/tutorial04/.

-Currently I'm on Part-4 of this tutorial.

However, it is showing an error while fetching a record from database table Poll as :

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

It shows an error :

ValueError at /polls/2/

invalid literal for int() with base 10: ''

Please help with the issue........as i am completely a newbie to this framework. I'm using MySql as my DBMS. This is how my urls.py looks like :


    from django.conf.urls import patterns, url

    from polls import views

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

Thanks in Advance

4
please add your urls.py - Hedde van der Heide
try casting poll_id to int - Amit Yadav
@am1ty9d9v: Yes i hv tried that....but no luck - dotslash
Then this means you are not getting the poll_id. Try to add a print poll_id' statement just after the def line to see what poll_id's got - Amit Yadav
@am1ty9d9v : I'm getting poll_id = 20 in place of 2 and 10 in place of 1.... - dotslash

4 Answers

4
votes

Change your url patterns to capture the pk element per the documentation. Django urls can capture named groups, hence poll_id

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

You need to check URL in your template. you need to pass the integer id to URL {{user.id}} because url need to have integer value in template.

Ex.  url:-  /polls/{{user.id}}/

Hope this will work for others.

0
votes

I had this error too.

My case it I had a typo in my form template. Double check the poll detail template (“polls/detail.html”) for typos.

-1
votes

simply replace

poll = get_object_or_404(Poll, pk=poll_id)

with

poll = get_object_or_404(Poll, pk=int(poll_id))