一尘不染

Django的大小写不敏感网址?

django

默认情况下,django的url解算器似乎执行区分大小写的搜索来解析url,并区分’/ Login’和’login’。我的网址格式如下。

urlpatterns = patterns('',
    (r'^admin/(.*)', admin.site.root),
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.STATIC_DOC_ROOT, 'show_indexes': True}),
    (r'^login/$', 'django.contrib.auth.views.login'),
    (r'^logout/$', do_logout),
)

谁能指导我,如何使Django URL不区分大小写?


阅读 455

收藏
2020-04-02

共1个答案

一尘不染

只需放在(?i)每个r'...'字符串的开头,即:

urlpatterns = patterns('',
(r'^(?i)admin/(.*)', admin.site.root),
(r'^(?i)static/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': settings.STATIC_DOC_ROOT, 'show_indexes': True}),
(r'^(?i)login/$', 'django.contrib.auth.views.login'),
(r'^(?i)logout/$', do_logout),
)

告诉每个RE不区分大小写地匹配

2020-04-02