The order of middleware affections the outcome.

Some middleware are dependant on the functionality of other middleware. For example a middleware that requires usage of request.session should come after the SessionMiddleware.

If we spot this issue in your GitHub pull request we give this advice:

settings.pymaintainabilityhigh
MIDDLEWARE =[
+
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
-
    "django.contrib.sessions.middleware.SessionMiddleware",
    ...
]

The order of middleware affections the outcome.

Some middleware are dependant on the functionality of other middleware. For example a middleware that requires usage of request.session should come after the SessionMiddleware.

Read more
Protect your pull requests from over 40 types of common Django technical debts with our GitHub code review bot.

Configuring this check

Django Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code middleware-order in your pyproject.toml file.

Read more about configuring Django Doctor.