python - 我需要帮助将 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'),
]
解决方案
您可以使用文档中描述的路径转换器。在您的情况下,您将需要int
and slug
,因此是这样的:
path('<slug:category_slug>/', views.product_list, name='product_list_by_category'),
path('<int:id>/<slug:slug>/', views.product_detail, name='product_detail'),
推荐阅读
- pandas - 将字典应用于包含字符串的数据框列
- swift - 为什么从登录页面到主页的转换不起作用(swift)?
- docker - 如何根据过去的使用情况删除旧的 Docker 镜像
- ray - Ray.Tune 的 PB2 在同一训练点的同一 actor 上始终失败,因为 Tune 代码返回 ValueError
- java - SpringBoot中的HttpServletRequest getRequestURI
- hadoop - Google 云上 dataproc gui 版本中的 Hadoop 作业错误
- c++ - 尝试在 C++ 应用程序中使用来自 ffmpeg 的 libavdevice 的 decklink 输出
- python - 有没有一种方法可以为句子否定执行自定义 TFIDF?
- python - 如何在 python-weka-wrapper 中使用回归?
- android - 允许用户在 Firestore 规则中首次尝试写入