[Django]-Django admin login suddenly demanding csrf token

48👍

Admin login normally does require a csrf token, but that’s normally all taken care for you.

  1. Check your browser’s cookies to see if there is a csrf token present
  2. Try clearing cookies and refreshing
  3. If you are using Django 4.0, you may to add this line to your settings.py file: CSRF_TRUSTED_ORIGINS = ['https://*.mydomain.com','https://*.127.0.0.1'] (making the appropriate changes). In 4.0, they started checking the origin header unlike in previous versions. Thanks to this answer for suggesting this solution.
  4. Check to make sure you have django.middleware.csrf.CsrfViewMiddleware in your middleware
  5. Check that you’re either on https or you have CSRF_COOKIE_SECURE=False (which is the default) in settings, otherwise your csrf cookie exists but won’t be sent. Purge your cookies after changing CSRF_COOKIE_SECURE.
👤ubadub

93👍

for new users facing this issue after upgrading to Django +4.0 you need to add CSRF_TRUSTED_ORIGINS=['https://*.YOUR_DOMAIN.COM'] to settings.py

thanks to the below answer:

https://stackoverflow.com/a/70326426/2259546

👤Zeedia

5👍

This error was appearing for me when I had not set CSRF_COOKIE_DOMAIN in my settings_local but it was set in my main settings.py.

In my case I set it to the local host eg

CSRF_COOKIE_DOMAIN = '127.0.0.1'

3👍

Add a csrf token to your context in the login view and in your template add in the hidden div for the csrf token. Ensure you have django.middleware.csrf.CsrfViewMiddleware in the middleware section in your settings.py.

Then add @csrf_protect to your views to do with login. It is also possible you tried to login with incorrect credentials – you need @csrf_protect on the logout view in your app’s views.py you call on the appropriate uri for login/logout etc. in urls.py also. My logout simply calls logout(request) then calls HttpResponseRedirect(”) which is probably not perfect but it does me for my needs for now.

👤Rodent

3👍

As a security measure, I had CSRF_COOKIE_SECURE = True in my settings. Trying to log into admin via localhost where there isn’t HTTPS threw the forbidden error.

Set it to False to get it working on localhost

👤KhoPhi

1👍

This could also happen when you are already logged in into your website hosted on a url different than admin. And then try to login into your admin panel in a new tab.
Try to open the admin panel in a different window.

👤r4v1

1👍

In my case it was solved by changing the setting:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

to

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'http')
👤Piero

0👍

Try opening your site in incognito mode.

There is a good chance that it could be your browser cookie, the above test will iron out that possibility.

-1👍

I used to have the same problem every time when I was using my default environment, and then using a virtual environment worked for me. It works every time. If you don’t know how to create a virtual environment, here’s how you do it:

  1. Just create a virtual environment in your project’s directory by
    running the command virtualenv theNameYouWannaGiveYourEnvironment.
  2. Then activate your virtual environment by using
    theNameYouWannaGiveYourEnvironment/bin/activate(on Linux, I think it works for Mac Os too, but it’s different for Windows).
  3. After that, just install Django by pip install django and all the other requirements for your application to run.

Alternatively, you can also use Anaconda to create your virtual environment and install all your requirements. Just refer to this documentation if you wanna use anaconda: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html

Leave a comment