升级到Django 1.11后,append_slash不再起作用 [英] After upgrade to Django 1.11 append_slash no longer works
问题描述
在Django 1.9(和Python 3.4)中,默认的APPEND_SLASH可以正常工作,即我可以输入'localhost:8000 / ideatree / videos'
并添加斜杠。
In Django 1.9 (and Python 3.4) the default of APPEND_SLASH worked correctly, i.e. I could enter 'localhost:8000/ideatree/videos' and the trailing slash would be added.
升级到Django 1.11(和Python 3.6)后,APPEND_SLASH不再起作用。
After an upgrade to Django 1.11 (and Python 3.6), APPEND_SLASH is no longer working.
我一直在寻找弃用通知,但是到目前为止,没有发现任何似乎适用的内容。 (旁边的问题:您如何像以前的版本一样重新打开大声弃用警告?)
I've looked for deprecation notices but so far found nothing that seems to apply. (side question: how do you turn 'loud deprecation warnings' back on, as they were in previous versions?)
这是我的主要urls.py:
Here is my main urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [ url(r'^(?i)ideatree/', include('ideatree.urls'),
name='home'),
]
以及包含的app_space中的urls.py:
and the urls.py from the included app_space:
from django.conf.urls import url
from . import views
app_name = 'ideatree'
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^(?i)features/$', views.features, name='features'),
url(r'^(?i)videos/$', views.videos, name='videos')
]
除了Django 1.9中,这两个url.py文件均未更改
Both these url.py files are unchanged except that in Django 1.9 I had
from django.conf.urls import patterns, include, url
在主urls.py中,但是现在不建议使用模式并发出警告。
in the main urls.py, but 'patterns' is now deprecated and throws a warning.
和以前一样,我没有根据其默认值True在settings.py中设置APPEND_SLASH,尽管我尝试将其默认值显式设置为True。
As before, I do not have APPEND_SLASH set in settings.py, relying on its default value of True, though I tried explicitly setting it to True with the same result.
这是我的中间件:
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
这里是错误:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/ideatree/videos
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^(?i)ideatree/ ^$ [name='index']
^(?i)ideatree/ ^(?i)features/$ [name='features']
^(?i)ideatree/ videos/$ [name='videos']
我还尝试清除浏览器缓存,并使用其他
I also tried clearing the browser cache, and using a different browser in case the cache still didn't get cleared.
登录到DEBUG级别或INFO级别的文件不会显示任何内容,一个空文件(警告:我的l ogging设置未经测试)。
Logging to file at level DEBUG or level INFO shows nothing, an empty file (warning: my logging setup is untested).
我必须忽略某些东西。
推荐答案
Django在Django 1.10中引入了新的中间件。如果您使用的是 MIDDLEWARE 设置= nofollow noreferrer>新型中间件和 MIDDLEWARE_CLASSES
(如果您使用的是旧式中间件。
Django introduced new middleware in Django 1.10. You should use the MIDDLEWARE
setting if you are using new-style middleware, and MIDDLEWARE_CLASSES
if you are using old-style middleware.
如果您使用的是Django 1.10或1.11,那么仍然支持旧的 MIDDLEWARE_CLASSES
设置,因此Django应该继续使用附加的斜杠进行重定向。
If you are using Django 1.10 or 1.11, then the old MIDDLEWARE_CLASSES
setting is still supported, so Django should continue to redirect with the appended slash.
但是,升级到Django 2.0后, MIDDLEWARE_CLASSES
设置将被忽略,您必须切换到 MIDDLEWARE
。
However, once you upgrade to Django 2.0, the MIDDLEWARE_CLASSES
setting is ignored and you must switch over to MIDDLEWARE
.
切换到 MIDDLEWARE
时,应删除 SessionAuthenticationMiddleware
,因为它在1.10和1.11中无效,在Django 2.0中已完全删除。
When you switch over to MIDDLEWARE
, you should remove SessionAuthenticationMiddleware
since it has no effect in 1.10 and 1.11, and is removed completely in Django 2.0.
这篇关于升级到Django 1.11后,append_slash不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!