1

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 tried adding the below code in my views but I'm getting this error:

 ValueError at /findme/

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

 Request Method:    GET
 Request URL:   http://127.0.0.1:8000/findme/
 Django Version:    1.4
 Exception Type:    ValueError
 Exception Value:   invalid literal for int() with base 10: 'id'

 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

Views

from meebapp.models import Meekme

def cribdetail(request):
    post=Meekme.objects.get(id='id')
    return render_to_response('postdetail.html',{'post':post, 'Meekme':Meekme},context_instance=RequestContext(request))

What I'm I missing?

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
picomon
  • 1,479
  • 4
  • 21
  • 38

2 Answers2

4

The problem is that 'id' is a string and you need to pass an integer here:

post=Meekme.objects.get(id='id')

It should most likely look like this:

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

where meekme_id is an integer that is part of the URL. Your URL configuration should contain:

url(r'^example/(?P<meekme_id>\d+)/$', 'example.views.cribdetail'),

When you visit example/3/, that means Django will call the view cribdetail with the value 3 assigned to meekme_id. See the Django URL documentation for more details.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • After following your instruction, I'm getting this error: NoReverseMatch at /search/ Reverse for 'meebapp.views.cribdetail' with arguments '()' and keyword arguments '{}' not found. Urlconf: url(r'^cribme/(?P\d+)/$', 'meebapp.views.cribdetail'), In Models: def get_absolute_url(self): return ('meebapp.views.cribdetail', (),[str(self.id)]) In Template: What I'm I missing? – picomon Jun 29 '12 at 10:16
  • @picomon: In that case you're giving incorrect parameters to the reverse function of Django. You need to make sure that the name and values for reversing a URL are correct and that there is indeed a URL with that name and those parameters. – Simeon Visser Jun 29 '12 at 10:19
  • Can I input id alone in the parameters? BTW: Do check my edit in the above comment. – picomon Jun 29 '12 at 10:21
  • Change the `get_absolute_url` to the following: `return ('meebapp.views.cribdetail', (), {'meekme_id': self.id})`. Also, in the template you need to add the id, so it would be `{% url meebapp.views.cribdetail post.id %}`. – Simeon Visser Jun 29 '12 at 10:25
  • another error: NoReverseMatch at /search/ Reverse for 'meebapp.views.cribdetail' with arguments '('',)' and keyword arguments '{}' not found – picomon Jun 29 '12 at 10:37
  • kindly check out my question here: http://stackoverflow.com/questions/11264655/valueerror-when-getting-objects-by-id – picomon Jun 29 '12 at 15:31
1

the error message is saying 'id' is integer but you are passing string .

zinking
  • 5,561
  • 5
  • 49
  • 81