Vincent
Vincent

Reputation: 51

CSRF token missing or incorrect, even after including the token tag

I am getting a 'CSRF token missing or incorrect' error, but I have already added the {% csrf_token%} tag in the web form. Any idea why I still face this error?

def index(request):
    if request.method == 'POST':
        form = RequestForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request, 'index.html')
    else:
        form = RequestForm()
    return render(request, 'index.html', {'form': form})`

I cannot post the template, but I use the token as shown in line below

<form id="reqForm" action="" method="POST" enctype="text/plain">{% csrf_token %}

Upvotes: 0

Views: 1355

Answers (1)

Alasdair
Alasdair

Reputation: 309039

It looks like your problem is enctype="text/plain" in your form. The CSRF protection assumes that the post data is form encoded.

The simplest fix is to remove that entirely, which is equivalent to:

enctype="enctype=application/x-www-form-urlencoded"

If you were uploading files in your form you would use:

enctype="multipart/form-data"

Upvotes: 2

Related Questions