Search code examples
djangonginxdjango-formsdjango-templatesdjango-admin

Error ```Forbidden (403) CSRF verification failed. Request aborted.``` when try to login in admin


The error occurs only on the remote server. Locally everything is fine. 4:19 4:20 seems to be no problem with static Didn't touch anything here:

ALLOWED_HOSTS = []
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

Using: django, nginx, docker, git actions. Where is trouble?

Another problem when I insert a link without an closed bracket (my-dns.com/admin) redirects me to 127.0.0.1/admin/ without port. when link is fine, trouble with csrf token


Solution

  • Part of the CSRF-token is, well, to prevent cross-site request forgery (that s of course what the intend is).

    So that means the CSRF middleware will also check the domain from which the request was made, these are the ALLOWED_HOSTS [Django-doc] in the settings.py file:

    # settings.py
    
    ALLOWED_HOSTS = ['foodgram-fklska.hopto.org']