python - 如何在Django2网址中以斜线传递参数

标签 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'),
]

访问以下任一失败:
  • mysite.com/test/test1/test2
  • 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.
    

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

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

    使用Django 2.0.2

    最佳答案

    默认的path converter <test_arg>(与<str:test_arg>等效)与正斜杠不匹配。

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

    关于python - 如何在Django2网址中以斜线传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49586991/

    相关文章:

    python - 如何替换字符串python中的正确字母

    python - Django url 模式顺序和正则表达式

    python - 查询某个字段没有 `None` 的所有对象

    python - 根据 Django 模型中的条件根据需要创建模型字段

    python - 我无法运行服务器 Django 2.0

    python - 从使用pivot_table()创建的df中删除索引名称

    python - Tensorflow:简单图像图像分类器在时代之间根本不更新

    Python Pyinstaller 没有生成 exe 文件

    python - django python 条件不起作用?

    python-3.x - 如何通过在 Django 2.0 中创建语言文件来集成多语言支持?