Middleware should be near the top

Incorrectly ordered middleware can stop the middleware working as intended.

The order of middleware affections the outcome.

Some middleware should be near the top of the middleware list to ensure it is one of the first to be called.

For example CommonMiddleware, LocaleMiddleware, and SecurityMiddleware should be near the top.

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

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

Incorrectly ordered middleware can stop the middleware working as intended.

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-top in your pyproject.toml file.

Read more about configuring Django Doctor.