1

我试图在我的 django 应用程序中通过 id 获取数据。问题是我不知道用户将点击的 id 类型。我尝试在我的视图中添加以下代码,但出现此错误:

 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

意见

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

我错过了什么?

4

2 回答 2

4

问题是这'id'是一个字符串,你需要在这里传递一个整数:

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

它很可能看起来像这样:

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

其中meekme_id是作为 URL 一部分的整数。您的 URL 配置应包含:

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

当您访问时example/3/,这意味着 Django 将调用cribdetail赋值为 3的视图meekme_id。有关更多详细信息,请参阅 Django URL 文档

于 2012-06-29T09:34:49.927 回答
1

错误消息说 'id' 是整数,但您正在传递 string 。

于 2012-06-29T09:33:15.340 回答