← Back to Django Course | Chapter 12: Signals & Middleware | Lesson 8 of 8

Built-in Middleware Overview

Django comes with a toolbox of ready-made checkpoints that handle security, sessions, and logins for you.

SecurityMiddleware

SecurityMiddleware adds a set of HTTP security headers to every response, such as protections against clickjacking and MIME-type sniffing.

Example: SecurityMiddleware

SecurityMiddleware adds a set of HTTP security headers to every response, such as protections against clickjacking and MIME-type sniffing.

markup
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
]
SECURE_BROWSER_XSS_FILTER = True
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

SessionMiddleware & AuthenticationMiddleware

SessionMiddleware attaches request.session, and AuthenticationMiddleware uses that session to attach request.user — together they power Django's login system.

Example: SessionMiddleware & AuthenticationMiddleware

SessionMiddleware attaches request.session, and AuthenticationMiddleware uses that session to attach request.user — together they power Django's login system.

markup
MIDDLEWARE = [
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

CsrfViewMiddleware

CsrfViewMiddleware checks that POST requests include a valid CSRF token, protecting forms from cross-site request forgery attacks.

Example: CsrfViewMiddleware

CsrfViewMiddleware checks that POST requests include a valid CSRF token, protecting forms from cross-site request forgery attacks.

markup
MIDDLEWARE = [
    "django.middleware.csrf.CsrfViewMiddleware",
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

CommonMiddleware

CommonMiddleware handles small conveniences like appending a trailing slash to URLs that are missing one, controlled by APPEND_SLASH.

Example: CommonMiddleware

CommonMiddleware handles small conveniences like appending a trailing slash to URLs that are missing one, controlled by APPEND_SLASH.

markup
MIDDLEWARE = [
    "django.middleware.common.CommonMiddleware",
]
APPEND_SLASH = True
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Removing SecurityMiddleware or CsrfViewMiddleware without understanding what protection they remove.
  2. Not realizing AuthenticationMiddleware is what makes request.user available in views.
  3. Assuming all built-in middleware is required — some, like GZipMiddleware, are optional performance add-ons.
Chapter Summary
  • SecurityMiddleware adds HTTP security headers like HSTS and content-type sniffing protection.
  • SessionMiddleware and AuthenticationMiddleware together make request.user and sessions work.
  • CsrfViewMiddleware protects POST forms from cross-site request forgery.
  • CommonMiddleware handles things like URL normalization.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.