ValueError when getting objects by id

后端 未结 4 1601
无人及你
无人及你 2021-01-23 10:19

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

4条回答
  •  攒了一身酷
    2021-01-23 10:46

    looks to me that your urlconf is to blame, it should be:

    url(r'^cribme/(?P\d+)/$', 'meebapp.views.cribdetail', name='cribdetail'),
    

    not:

    url(r'^cribme/(?P)\d+/$', 'meebapp.views.cribdetail', name='cribdetail'),
    

    ?P 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.

提交回复
热议问题