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 our GitHub code review bot spots this issue in your pull request it gives this advice:

django-doctorbotsuggested changes just now
settings.py
1
+
MIDDLEWARE =[
2
+
    "django.middleware.security.SecurityMiddleware",
3
+
    "django.contrib.sessions.middleware.SessionMiddleware",
4
+
    "django.contrib.redirects.middleware.RedirectFallbackMiddleware",
Suggested changes
-
    "django.contrib.redirects.middleware.RedirectFallbackMiddleware",
+
    "django.contrib.redirects.middleware.RedirectFallbackMiddleware",
Commit suggestion

Incorrectly ordered middleware can stop the middleware working as intended.

Read more
5
+
    "django.middleware.common.CommonMiddleware",
6
+
    "django.contrib.auth.middleware.AuthenticationMiddleware",
7
+
    "django.contrib.messages.middleware.MessageMiddleware",
8
+
]
Update settings.py
We're your Django code review copilot. Get code improvements right in your pull request with our GitHub code review bot.

Configuring this check

Code Review 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 Code Review Doctor.