After upgrade to Django 1.11 append_slash no longe

2019-07-12 02:19发布

问题:

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.

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?)

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

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')
]

Both these url.py files are unchanged except that in Django 1.9 I had

from django.conf.urls import patterns, include, url

in the main urls.py, but 'patterns' is now deprecated and throws a warning.

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.

Here's my middleware:

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',
]

Here's the error:

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.

Logging to file at level DEBUG or level INFO shows nothing, an empty file (warning: my logging setup is untested).

There's got to be something I'm overlooking.

回答1:

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.

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.

However, once you upgrade to Django 2.0, the MIDDLEWARE_CLASSES setting is ignored and you must switch over to MIDDLEWARE.

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.