如何添加包含斜杠的动态 URL 部件



我在 Django 项目中的URL中使用动态部分,如<str:item_code>,有时 str 包含一个斜杠/,这会导致找不到错误。

这是我的 URL 模式的样子:

path('find/the/item/<str:item_description>/', views.find_the_item, name="find_the_item"),

有没有强制 URL 忽略此<str:item_description>部分内的所有斜杠?

我不熟悉 Django,但阅读文档看起来好像您可以使用指定的path而不是str

path('find/the/item/<path:item_description>/', views.find_the_item, name="find_the_item"),

path说明符"匹配任何非空字符串,包括路径分隔符'/'。这允许您匹配完整的 URL 路径,而不仅仅是 URL 路径的一部分,如 str."。

(来自 https://docs.djangoproject.com/en/2.2/topics/http/urls/#path-converters(

最新更新