我URL
在我的 Django 项目中使用动态部分,因为<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路径,而不仅仅是一个与STR一个URL路径的部分。”
(来自https://docs.djangoproject.com/en/2.2/topics/http/urls/#path-converters)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句