【问题标题】:The "/" at the end of a url网址末尾的“/”
【发布时间】:2015-01-27 17:15:51
【问题描述】:

我发现了一件我不知道如何解决的有趣的事情:

urlpatterns = patterns('',
    url(r'^$', 'my_app1.views.index', name='index'),
    url(r'^path1/path2/', 'my_app1.api.method1')
    // or url(r'^path1/path2', 'my_app1.api.method1')
)

我不希望网址末尾包含“/”。

如果我转到http://localhost:8000/path1/path2,它会将我重定向到http://localhost:8000/path1/path2/,末尾带有“/”。无论我做什么来摆脱网址末尾的“/”,我都失败了。与 Rails 不同,似乎没有办法这样做。是真的吗?

【问题讨论】:

    标签: python regex django django-urls


    【解决方案1】:

    你可以使用settings.APPEND_SLASH控制它。

    默认情况下,它是 True。在项目设置文件中将值更改为 False。

    APPEND_SLASH = False
    

    但是如果你设置了,访问页面/path/path2会导致404错误。您需要更改 url 模式:

    url(r'^path1/path2/?', 'my_app1.api.method1')  # To match url with/without trailing /
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 2015-03-03
      • 2011-08-21
      相关资源
      最近更新 更多