【发布时间】:2012-07-01 03:42:28
【问题描述】:
我试图在我的 django 应用程序中按 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))
网址配置
url(r'^cribme/(?P<meekme_id>)\d+/$', 'meebapp.views.cribdetail', name='cribdetail'),
在模板中:
<a href="{% url cribdetail post.id %}">{{ result.object.title }}</a>
当我点击模板中的上述链接时,出现以下错误:
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
一直在与此作斗争。我怎样才能摆脱这个错误?
【问题讨论】:
-
@DanielRoseman:不完全是,这里有不同的问题,正则表达式有错误(空组)。
-
另外,直接使用'get'方法来获取对象其实并不是一个好主意。最佳做法是使用 django 快捷方式 get_object_or_404。
标签: python django django-views django-urls