← All notes

Django CSRF protection uses a cookie and a hidden form field

Answer: Django's CSRF middleware and template tag protect against cross-site request forgeries: a malicious site triggering an action on your site with the browser credentials of a logged-in user.

Cookie and form field

Protection is based on a CSRF cookie whose value is a random secret other sites cannot access, plus a hidden form field named csrfmiddlewaretoken on outgoing POST forms. The field value is a masked form of the secret and changes per response. Incoming requests other than GET, HEAD, OPTIONS, or TRACE must present both a cookie and a correct field, or they get a 403.

Middleware

CsrfViewMiddleware requires a present and correct csrfmiddlewaretoken for POST requests that have a CSRF cookie, and it sets an outgoing CSRF cookie. Use it together with the {% csrf_token %} template tag.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources and notices: Django documentation and source (django/django): Copyright (c) Django Software Foundation and individual contributors; BSD-3-Clause (https://raw.githubusercontent.com/django/django/stable/6.1.x/LICENSE). Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Django docs, Cross Site Request Forgery protection, BSD-3-Clause (django/django); Django source, django.middleware.csrf.CsrfViewMiddleware, BSD-3-Clause (django/django). Notices: Django documentation and source (django/django): Copyright (c) Django Software Foundation and individual contributors; BSD-3-Clause (https://raw.githubusercontent.com/django/django/stable/6.1.x/LICENSE).

Cite this

Permanent ID: iil:note:9b88d3sg

iinventedlight. 2026. "Django CSRF protection uses a cookie and a hidden form field." iinventedlight Library, iil:note:9b88d3sg, version of 2026-11-10. https://iinventedlight.com/library/notes/django-csrf/. License: CC-BY-4.0.

@misc{iil_note_9b88d3sg,
  author = {{iinventedlight}},
  title = {{Django CSRF protection uses a cookie and a hidden form field}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-10},
  url = {https://iinventedlight.com/library/notes/django-csrf/},
  note = {Permanent ID iil:note:9b88d3sg. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/django-csrf.md.