Django通过覆盖regex p交换URL

2024-06-16 11:53:38 发布

您现在位置:Python中文网/ 问答频道 /正文

当请求[GET]127.0.0.1:8000/restaurant/1时,我得到了一个干净的json和200状态代码

urlpatterns = [

url(r'^restaurant',views.Restaurant_List_Create.as_view(), name='all_restaurants'),
url(r'^restaurant/(?P<pk>\d+)',views.Restaurant_Retrive.as_view(), name='specified_restaurant'),

]

但当我交换url代码时,它会运行views.Restaurant\u List\u Create.as\u view()(覆盖正则表达式url)

urlpatterns = [

url(r'^restaurant/(?P<pk>\d+)',views.Restaurant_Retrive.as_view(), name='specified_restaurant'),    
url(r'^restaurant',views.Restaurant_List_Create.as_view(), name='all_restaurants'),


]

Tags: 代码nameviewurlascreateallrestaurant
1条回答
网友
1楼 · 发布于 2024-06-16 11:53:38

您的url两者都匹配,因为您没有在url末尾包含$sign

您可以按如下方式更改它们:

urlpatterns = [
  url(r'^restaurant/(?P<pk>\d+)$',views.Restaurant_Retrive.as_view(), name='specified_restaurant'),    
  url(r'^restaurant$',views.Restaurant_List_Create.as_view(), name='all_restaurants'),
]

相关问题 更多 >