Free auditStart free
GuideError · HTTP status codes

403 Forbidden in Django

Find why Django returns 403: CSRF verification failing, a raised PermissionDenied, or a proxy in front. Includes CSRF_TRUSTED_ORIGINS and fetch header fixes.

4 min
read
5
sources
Oct 29, 2026
updated
Scaplet Editorial Team
author
The short answer

Django returns 403 Forbidden for two main reasons: CsrfViewMiddleware rejected an unsafe request (POST, PUT, PATCH, DELETE) because the token, Origin or Referer check failed, or your code raised PermissionDenied. Read the reason Django logs for CSRF failures, then fix the token, the trusted origins or the permission check it names.

  1. Where Django produces a 403
  2. Reproduce and check it in 2 minutes
  3. CSRF failures and their fixes
  4. PermissionDenied and custom 403 pages
  5. What it means for SEO and crawlers
30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
02Check it

Reproduce and check it in 2 minutes

First confirm the 403 is a plain status response and not something your browser added. This command hits a public endpoint that always answers 403, so you can compare headers with your own site:

Then replay the failing request against your own server with no cookies and no token. This needs your project running locally (adjust the path to a form view):

Your own dev server: a bare POST should be refused by CSRF
python manage.py runserver
# in another terminal
curl -i -X POST http://127.0.0.1:8000/your-form/ -d 'name=test'
# Expect: HTTP/1.1 403 Forbidden. Check the runserver console for the CSRF warning
# with its reason text.

If that POST returns 403 and your browser form returns 200, the browser is sending something the curl call does not. If both return 403, read the reason text before changing anything.

Public endpoint: expect HTTP 403 and an empty-ish body
curl -i https://httpbin.org/status/403
# Expect: HTTP/2 403 followed by headers. Run the same curl -i against your URL
# and compare the Server and Via headers to find which layer answered.
03Fix it on your stack

CSRF failures and their fixes

The reason string tells you which check failed. These are the strings defined in django/middleware/csrf.py:
Form: include the tag
<form method="post">
  {% csrf_token %}
  <button type="submit">Save</button>
</form>
fetch with the CSRF header
const request = new Request(
    /* URL */,
    {
        method: 'POST',
        headers: {'X-CSRFToken': csrftoken},
        mode: 'same-origin' // Do not send CSRF token to another domain.
    }
);
fetch(request).then(function(response) {
    // ...
});
settings.py: trusted origins
CSRF_TRUSTED_ORIGINS = ["https://subdomain.example.com", "https://*.example.com"]
CSRF reason strings and what to change
ReasonUsual causeFix
Origin checking failed - %s does not match any trusted origins.The Origin header is a different origin from the one Django sees, often behind HTTPS termination or on another subdomainAdd the full origin, scheme included, to CSRF_TRUSTED_ORIGINS
Referer checking failed - no Referer.A secure request carried neither Origin nor RefererStop stripping the Referer header, or send Origin
Referer checking failed - Referer is insecure while host is secure.A page on http posting to an https hostServe the form over https
CSRF cookie not set.No csrftoken cookie was sentRender a template with csrf_token, or use ensure_csrf_cookie on the view
CSRF token missing.The cookie was present but the form field or header was notAdd the token to the form or the X-CSRFToken header

In a template form, the token goes inside the form element:

For JavaScript, the Django how-to reads the csrftoken cookie and sends it in the X-CSRFToken header with mode: 'same-origin' so it is not sent to another domain:

If your form lives on another origin, the Django settings reference says to list that origin. Its wildcard form allows all subdomains:

04Check it

PermissionDenied and custom 403 pages

PermissionDenied is raised when a user lacks permission for the requested action. Your own permission checks can raise it directly:

The views documentation's testing example shows the matching hook: set handler403 in the root URLconf and the exception is rendered by your function. Handlers set anywhere else have no effect.

urls.py: custom 403 handler
from django.http import HttpResponse

def response_error_handler(request, exception=None):
    return HttpResponse("Error handler content", status=403)

handler403 = response_error_handler

For CSRF specifically, the how-to says the default error page is not friendly and names the CSRF_FAILURE_VIEW setting as the place to supply your own. Return an HttpResponseForbidden from it.

Raise it from a view
from django.core.exceptions import PermissionDenied

def edit_invoice(request, pk):
    invoice = get_object_or_404(Invoice, pk=pk)
    if invoice.owner_id != request.user.id:
        raise PermissionDenied
    ...
05Explained

What it means for SEO and crawlers

Google treats 4xx codes other than 429 the same way: content from those URLs is ignored. It also says "Don't use 401 and 403 status codes for limiting the crawl rate." CSRF only applies to unsafe methods, so a CSRF 403 will not hit a crawler's GET request. A 403 on a public GET comes from PermissionDenied, middleware you added, or a firewall in front. If pages you want indexed return it, fix the rule rather than waiting. See 4xx errors and the guide for a 404 in Django.
Questions

Asked a lot.

3 questions people ask next, answered from the sources on this page.

01Why does Django return 403 on POST only?

CsrfViewMiddleware checks unsafe methods such as POST, so a GET to the same URL succeeds while the POST fails the Origin, Referer or token check. Include the token in the form or header and add any cross-origin site to CSRF_TRUSTED_ORIGINS.

02How do I see why Django rejected a CSRF request?

CSRF failures are logged as warnings to the django.security.csrf logger, and the reason is passed to the CSRF failure view. Match the reason to the table above.

03Is csrf_exempt a safe fix for a 403?

It removes the middleware check for that view, so it only suits endpoints that do not depend on browser cookies. For browser forms, fix the token or the trusted origin instead.

Sources · 5

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • djangoproject.com
  • github.com
  • google.com
  1. 01How to use Django's CSRF protectiondocs.djangoproject.com · checked 2026-11-01
  2. 02Django settings: CSRF_TRUSTED_ORIGINS and CSRF_FAILURE_VIEWdocs.djangoproject.com · checked 2026-11-01
  3. 03Django source: django/middleware/csrf.pygithub.com · checked 2026-11-01
  4. 04Django views: custom error handlersdocs.djangoproject.com · checked 2026-11-01
  5. 05How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-11-01
Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane