This page: https://docs.djangoproject.com/en/1.7/ref/middleware/#middleware-ordering has/implies the following ordering (Question #1: Am I correct in assuming the list is in correct order?)
while the graphic at https://docs.djangoproject.com/en/dev/topics/http/middleware/#hooks-and-application-order seems to indicate that CommonMiddleware should be before SessionMiddleware:
In Django 1.5 django-admin.py startproject
generates (https://docs.djangoproject.com/en/1.5/topics/http/middleware/#hooks-and-application-order)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
while versions starting with 1.6 generate (https://docs.djangoproject.com/en/1.6/topics/http/middleware/#hooks-and-application-order)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
Question #2: Should I change the order in my 1.5 projects so SessionMiddleware is before CommonMiddleware?
Question #3: How will requests be processed differently when switching the order?
Middleware is a framework of hooks into Django's request/response processing. It's a light, low-level “plugin” system for globally altering Django's input or output. Each middleware component is responsible for doing some specific function.
Middleware is like a middle ground between a request and response. It is like a window through which data passes. As in a window, light passes in and out of the house. Similarly, when a request is made it moves through middlewares to views, and data is passed through middleware as a response.
Create a file named custom_middleware.py (or anything which you like) and a regular Python function / class in it. You can write middleware as a function or as a class whose instances are callable.
FYI, you may be interested in reading Django 1.7 Middleware Ordering. It explains the consequences of the middleware ordering, and I think it still applies on previous Django versions.
Middleware ordering Here are some hints about the ordering of various Django middleware classes:
UpdateCacheMiddleware
Before those that modify the Vary header (SessionMiddleware, GZipMiddleware, LocaleMiddleware).
GZipMiddleware
Before any middleware that may change or use the response body.
After UpdateCacheMiddleware: Modifies Vary header.
ConditionalGetMiddleware
Before CommonMiddleware: uses its Etag header when USE_ETAGS = True.
SessionMiddleware
After UpdateCacheMiddleware: Modifies Vary header.
LocaleMiddleware
One of the topmost, after SessionMiddleware (uses session data) and CacheMiddleware (modifies Vary header).
CommonMiddleware
Before any middleware that may change the response (it calculates ETags).
After GZipMiddleware so it won’t calculate an ETag header on gzipped contents.
Close to the top: it redirects when APPEND_SLASH or PREPEND_WWW are set to True.
The relevant change seems to be Enabled the locale middleware by default.
In the comments from that commit:
+.. versionchanged:: 1.6 + In previous versions,
LocaleMiddleware` wasn't enabled by default. + +Because middleware order matters, you should follow these guidelines: * Make sure it's one of the first middlewares installed. * It should come after
SessionMiddleware, because
LocaleMiddlewaremakes use of session data. And it should come before
CommonMiddlewarebecause
CommonMiddlewareneeds an activated language in order to resolve the requested URL. * If you use
CacheMiddleware, put
LocaleMiddleware`` after it.
So, in fact the new changes in ordering is to resolve enabling LocaleMiddleWare by default. You shouldn't need to change these orders in previous Django
's versions because it's not enabled by default.
LocaleMiddleware
has been removed from the project template
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With