Jazi
Jazi

Reputation: 6712

Django: CSRF verification failed

Don't know how to resolve this problem :/.

views.py:

# coding: utf-8
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic.simple import direct_to_template
from django.core.mail import send_mail
from django.template import Context, loader
from django.conf import settings
from sklep.models import Produkt
from sklep.forms import ZamowienieForm
from django.core.context_processors import csrf

def koszyk(request):
    koszyk = request.session.get('koszyk', [])
    produkty = list(Produkt.objects.filter(pk__in=koszyk))

    if request.method == 'POST':
        formularz = ZamowienieForm(request.POST)

        if formularz.is_valid():
            dane = formularz.cleaned_data
            tresc = loader.get_template('sklep/zamowienie.txt').render(Context({'produkty': produkty, 'dane': dane}))

            send_mail('Potwierdzenie zakupu', tresc, settings.EMAIL_SKLEPU, [dane['email']])
            send_mail(u'Zamówienie', tresc, dane['email'], [settings.EMAIL_SKLEPU])

            del request.session['koszyk']

            return HttpResponseRedirect(reverse('sklep_koszyk'))
    else:
        formularz = ZamowienieForm()

    if koszyk:
        kontekst = {'koszyk': produkty, 'formularz': formularz}
    else:
        kontekst = {'koszyk': []}

    return direct_to_template(request, 'sklep/koszyk.html', extra_context = kontekst)

def koszyk_dodaj(request, id_produktu):
    koszyk = request.session.get('koszyk', [])
    if int(id_produktu) not in koszyk:
        koszyk.append(int(id_produktu))
    request.session['koszyk'] = koszyk
    return HttpResponseRedirect(reverse('sklep_koszyk'))

forms.py

# coding: utf-8
from django import forms
from django.contrib.localflavor.pl.forms import PLPostalCodeField

class ZamowienieForm(forms.Form):
    email = forms.EmailField()
    imie_nazwisko = forms.CharField(label=u'Imię i nazwisko', max_length=60)
    adres = forms.CharField(max_length=100)
    kod_pocztowy = PLPostalCodeField()
    miasto = forms.CharField(max_length=60)
    uwagi = forms.CharField(widget=forms.Textarea, required=False)

Upvotes: 0

Views: 1388

Answers (4)

Imran A
Imran A

Reputation: 1

Assuming your templates contain {% csrf_token%} already, make sure to add these lines in your project/settings.py 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

Ulrich Dangel
Ulrich Dangel

Reputation: 4625

Django requires for POST request a CSRF token to protect against Cross Site Request Forgeries. You probably have to include {% csrf_token %} in your template inside the form. If you know what you are doing (for example api calls) you can also use the @csrf_exempt decorator.

For more details about the issue have a look at the Django documentation about CSRF Protection

Upvotes: 0

Paul W
Paul W

Reputation: 51

Also, to add to what murgatroid99 said, you need to include the csrf(request) when you are calling that page. I usually do a locals().update(csrf(request)) then call the page with locals() as the global dictionary parameter.

Upvotes: 0

murgatroid99
murgatroid99

Reputation: 20232

The problem is probably in your template sklep/koszyk.html. Inside of the form on the html page you need to include {% csrf_token %}. The documentation has more information about this feature.

Upvotes: 2

Related Questions