I’m trying to get data by id in my django app. The problem is that I don’t know the kind of id the user will click on. I input the below codes in views.
Views
def cribdetail(request, meekme_id):
post=Meekme.objects.get(id=meekme_id)
return render_to_response('postdetail.html',{'post':post, 'Meekme':Meekme},context_instance=RequestContext(request))
Urlconf
url(r'^cribme/(?P<meekme_id>)\d+/$', 'meebapp.views.cribdetail', name='cribdetail'),
In template:
<a href="{% url cribdetail post.id %}">{{ result.object.title }}</a>
When I click on the above link in my template, I’m getting the below error:
ValueError at /cribme/0/
invalid literal for int() with base 10: ''
Request Method: GET
Request URL: http://127.0.0.1:8000/cribme/0/
Django Version: 1.4
Exception Type: ValueError
Exception Value:
invalid literal for int() with base 10: ''
Exception Location: C:\Python27\lib\site-packages\django\db\models\fields\__init__.py in get_prep_value, line 537
Python Executable: C:\Python27\python.exe
Python Version: 2.7.3
Been fighting with this for a while now. How can I get rid of that error?
looks to me that your urlconf is to blame, it should be:
not:
?P<meekme_id>means “give the matched string between parentheses this name. () matches nothing, which is why your app give an error when trying to look up the item with id''.When the parentheses enclose the
\d+, you match a natural number, which should work.