我想通過我的django應用程序中的id獲取數據。問題是我不知道用戶點擊的id的種類。我嘗試添加在我的意見下面的代碼,但我得到這個錯誤:在Django中通過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))
我我缺少的是什麼?
按照您的說明操作後,我收到此錯誤:NoReverseMatch at/search/ 未找到參數'()'和關鍵字參數'{}'的'meebapp.views.cribdetail'反向。 Urlconf:url(r'^ cribme /(?P \ d +)/ $','meebapp.views.cribdetail'),在模型中:def get_absolute_url(self): return('meebapp.views.cribdetail', ),[str(self.id)]]在模板中:我錯過了什麼? –
picomon
@picomon:在這種情況下,你會給Django的反向函數提供不正確的參數。您需要確保反轉URL的名稱和值是正確的,並且確實存在具有該名稱和這些參數的URL。 –
我可以在參數中單獨輸入ID嗎?順便說一句:檢查我的編輯在上面的評論。 – picomon