mp3909
mp3909

Reputation: 19

Python Django giving me Forbidden (403) CSRF verification failed. Request aborted

Django Version = 2.2.2 This problem appears to happen on using Google Chrome.

I have added {% csrf_token %} inside all my form tags like this:

{% extends 'base.html' %}

{% block content %}

<form method="post">
    {% csrf_token %}            #-------------> here it is
    {% for field in login_form %}
        <p>
            {{field.label_tag}}
            {{field}}

            {% if field.help_text %}
                {field.help_text}}
            {% endif %}
        </p>
    {% endfor %}

    {% for field in login_form %}
        {% for error in field.errors %}
            <p>{{error}}</p>
        {% endfor %}
    {% endfor %}


    {% if login_form.non_field_errors %}
        <p>{{login_form.non_field_errors}}</p>
    {% endif %}

    <input type="submit" value="Login"> 
</form>

{% endblock content %}

But when I actually try to login using that form and click the submit button, it should then redirect me to the home page. However, instead it is giving me the below error message:

*Forbidden (403) CSRF verification failed. Request aborted. Help Reason given for failure: CSRF token missing or incorrect.

In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure: Your browser is accepting cookies. The view function passes a request to the template's render method. In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL. If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data. The form has a valid CSRF token. After logging in in another browser tab or hitting the back button after a login, you may need to reload the page with the form, because the token is rotated after a login. You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed. You can customize this page using the CSRF_FAILURE_VIEW setting.*

Upvotes: 0

Views: 5535

Answers (2)

Imran A
Imran A

Reputation: 1

Please make sure these lines are included your settings file:

 CSRF_TRUSTED_ORIGINS = [
    'http://localhost:8000',
    'http://*youripaddress*',   
    ]

ALLOWED_HOSTS = [
    'localhost',
    'http://*youripaddress*',
    ]

CORS_ORIGIN_WHITELIST = [
    'http://localhost:8000',
    'http://*youripaddress*',
    ]

By default Django includes ALLOWED_HOST option when generating a project, however trusted list and whitelist options need to be added manually.

Upvotes: 0

yum
yum

Reputation: 1263

This is the error you are facing: https://docs.djangoproject.com/en/3.1/ref/csrf/#rejected-requests

You could use the @csrf_exempt decorator to prevent the csrf errors and with it you could remove the {% csrf_token %} altogether. Check this: https://docs.djangoproject.com/en/3.0/ref/csrf/#django.views.decorators.csrf.csrf_exempt

Upvotes: 1

Related Questions