如何在 Django2 urls 中使用斜杠传递参数

Hen*_*son 9 python django django-2.0

我想使用包含正斜杠的参数调用 Django URL。例如,我想mysite.com/test/'test1/test2'. (例如,天真地,网址是mysite.com/test/test1/test2

urls.py

urlpatterns = [
    path('test/<test_arg>/', views.test, name='test'),
]
Run Code Online (Sandbox Code Playgroud)

访问以下任一失败:

  1. mysite.com/test/test1/test2
  2. mysite.com/test/test1%2Ftest2 (%2F 是正斜杠的标准 URL 编码)

出现以下错误:

错误:

Using the URLconf defined in test.urls, Django tried these URL patterns, in this order:

    reader/ test/<test_arg>/
    admin/

The current path, test/test1/test2/, didn't match any of these.
Run Code Online (Sandbox Code Playgroud)

我希望使用 1 中的路径失败,但我很惊讶 2 中的路径失败。

解决这个问题的正确方法是什么?

使用 Django 2.0.2

Ala*_*air 13

默认路径转换器 <test_arg>(相当于<str:test_arg>)与正斜杠不匹配。

使用<path:test_arg>匹配正斜杠。