首页 > 解决方案 > 我需要帮助将 urlpatterns url 转换为它们的等效路径

问题描述

我正在帮助一个朋友完成一个项目,但我无法将 urlpatterns url 转换为他们的路径等价物。有什么帮助吗?

我已经完成了第一部分。

path('store', views.product_list, name='product_list'),

但其余的似乎具有挑战性

urlpatterns = [
    url(r'^store', views.product_list, name='product_list'),
    url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),
    url(r'^(?P<id>\d+)/(?P<slug>[-\w]+)/$', views.product_detail, name='product_detail'),
]

标签: pythondjango

解决方案


您可以使用文档中描述的路径转换器。在您的情况下,您将需要intand slug,因此是这样的:

path('<slug:category_slug>/', views.product_list, name='product_list_by_category'),
path('<int:id>/<slug:slug>/', views.product_detail, name='product_detail'),

推荐阅读