dotslash
dotslash

Reputation: 2061

Django - ValueError: invalid literal for int() with base 10: ''

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

Upvotes: 2

Views: 5992

Answers (4)

Wagh
Wagh

Reputation: 4306

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.

Upvotes: 2

Cian O'Flynn
Cian O'Flynn

Reputation: 31

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.

Upvotes: 0

Bit68
Bit68

Reputation: 1546

simply replace

poll = get_object_or_404(Poll, pk=poll_id)

with

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

Upvotes: -1

Hedde van der Heide
Hedde van der Heide

Reputation: 22449

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

Upvotes: 4

Related Questions