# 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.

Canonical: https://scaplet.com/errors/http/403/django

Guide Error · HTTP status codes

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

403 4xx · Client error

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](#where-django-produces-a-403)
2. [Reproduce and check it in 2 minutes](#reproduce-and-check-it-in-2-minutes)
3. [CSRF failures and their fixes](#csrf-failures-and-their-fixes)
4. [PermissionDenied and custom 403 pages](#permissiondenied-and-custom-403-pages)
5. [What it means for SEO and crawlers](#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 with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F403%2Fdjango%20(%22403%20Forbidden%20in%20Django%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F403%2Fdjango%20(%22403%20Forbidden%20in%20Django%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F403%2Fdjango%20(%22403%20Forbidden%20in%20Django%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F403%2Fdjango%20(%22403%20Forbidden%20in%20Django%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

_01_Which layer returned the 403

## Where Django produces a 403

A 403 means the server understood the request and refused it (see [403 Forbidden](/errors/http/403) for the general meaning). Django has very few places that answer 403 on their own, which makes the search short.

Source

### CsrfViewMiddleware

Trigger

An unsafe request failed the Origin, Referer or token check

How to tell

The CSRF failure view runs; the reason goes to the django.security.csrf logger as a warning

Source

### PermissionDenied

Trigger

Your view, a decorator or a mixin raised it

How to tell

A stack trace through your own permission code; handler403 renders the response

Source

### Proxy, CDN or web server

Trigger

A firewall, ACL or hotlink rule refused the request

How to tell

The page is not Django's; the request never reaches the app log

Request path through a Django project

```
browser -> proxy / load balancer -> WSGI or ASGI server -> middleware (CsrfViewMiddleware checks Origin/Referer + token) -> 403 CSRF -> URL resolver -> view -> view or decorator raises PermissionDenied -> 403 handler -> response (proxy may add its own 403 before or after Django)
```

The Django CSRF [how-to](/glossary/how-to-guide) states the default: "By default, a '403 Forbidden' response is sent to the user if an incoming request fails the checks performed by `CsrfViewMiddleware`." It adds that this usually means a real cross-site forgery or a programming error where the token was left out of a POST form.

_02_Check 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.
```

_03_Fix 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: include the tag

```
<form method="post"> {% csrf_token %} <button type="submit">Save</button> </form>
```

fetch with the CSRF header
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
settings.py: trusted origins

```
CSRF_TRUSTED_ORIGINS = ["https://subdomain.example.com", "https://*.example.com"]
```

CSRF reason strings and what to change
| Reason | Usual cause | Fix |
| 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](/glossary/https) termination or on another subdomain | Add the full origin, scheme included, to CSRF_TRUSTED_ORIGINS |
| Referer checking failed - no Referer. | A secure request carried neither Origin nor Referer | Stop 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 host | Serve the form over https |
| CSRF cookie not set. | No csrftoken cookie was sent | Render 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 not | Add 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:

**Do not reach for csrf_exempt first** `csrf_exempt` marks a view as exempt from the middleware's protection. It makes the 403 disappear, and the check it removes is the one that stops forged POSTs. Use it only for endpoints that do not rely on browser cookies, such as server-to-server webhooks.

_04_Check 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 ...
```

_05_Explained

## 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](/seo-issues/4xx-errors) and the guide for a [404 in Django](/errors/http/404/django).

Questions

## Asked a lot.

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

_01_ Why 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.

_02_ How 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.

_03_ Is 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. 01 [How to use Django's CSRF protection](https://docs.djangoproject.com/en/stable/howto/csrf/) docs.djangoproject.com · checked 2026-11-01
2. 02 [Django settings: CSRF_TRUSTED_ORIGINS and CSRF_FAILURE_VIEW](https://docs.djangoproject.com/en/stable/ref/settings/#csrf-trusted-origins) docs.djangoproject.com · checked 2026-11-01
3. 03 [Django source: django/middleware/csrf.py](https://github.com/django/django/blob/main/django/middleware/csrf.py) github.com · checked 2026-11-01
4. 04 [Django views: custom error handlers](https://docs.djangoproject.com/en/5.1/topics/http/views/) docs.djangoproject.com · checked 2026-11-01
5. 05 [How HTTP status codes affect Google's crawlers](https://developers.google.com/crawling/docs/troubleshooting/http-status-codes) developers.google.com · checked 2026-11-01

## Keep learning

[All errors](/errors)

[Learn 400 Bad Request in Django HTTP status codes](/errors/http/400/django) [Related 4xx errors Status codes and links](/seo-issues/4xx-errors) [Read next 404 Not Found in Django HTTP status codes](/errors/http/404/django) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 403 errors
[403 Forbidden](/errors/http/403) [403 Forbidden in Apache](/errors/http/403/apache) [403 Forbidden in Chrome](/errors/http/403/chrome) [403 Forbidden in IIS](/errors/http/403/iis) [403 Forbidden in Laravel](/errors/http/403/laravel)

More in 403 Forbidden
[403 Forbidden in Apache](/errors/http/403/apache) [403 Forbidden in Chrome](/errors/http/403/chrome) [403 Forbidden in Django](/errors/http/403/django) [403 Forbidden in IIS](/errors/http/403/iis) [403 Forbidden in Laravel](/errors/http/403/laravel) [403 Forbidden in nginx](/errors/http/403/nginx) [403 Forbidden in Spring Boot](/errors/http/403/spring-boot) [403 Forbidden in WordPress](/errors/http/403/wordpress) [403 Forbidden on Cloudflare](/errors/http/403/cloudflare) [403 Forbidden on CloudFront](/errors/http/403/cloudfront)

Terms on this page
[How-to guide](/glossary/how-to-guide) [HTTPS](/glossary/https)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

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

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
