简体   繁体   English

Django:CSRF验证失败

[英]Django: CSRF verification failed

Don't know how to resolve this problem :/. 不知道如何解决这个问题:/。

views.py: 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)

The problem is probably in your template sklep/koszyk.html. 问题可能出在您的模板sklep / koszyk.html中。 Inside of the form on the html page you need to include {% csrf_token %} . 在html页面上的表单内部,您需要包含{% csrf_token %} The documentation has more information about this feature. 文档具有有关此功能的更多信息。

Also, to add to what murgatroid99 said, you need to include the csrf(request) when you are calling that page. 另外,要添加murgatroid99所说的内容,您需要在调用该页面时包括csrf(request)。 I usually do a locals().update(csrf(request)) then call the page with locals() as the global dictionary parameter. 我通常会执行locals()。update(csrf(request)),然后使用locals()作为全局字典参数来调用页面。

Django requires for POST request a CSRF token to protect against Cross Site Request Forgeries . Django要求POST请求CSRF令牌以防止跨站点请求伪造 You probably have to include {% csrf_token %} in your template inside the form. 您可能必须在表单中的模板中包含{% csrf_token %} If you know what you are doing (for example api calls) you can also use the @csrf_exempt decorator. 如果您知道自己在做什么(例如api调用),则也可以使用@csrf_exempt装饰器。

For more details about the issue have a look at the Django documentation about CSRF Protection 有关此问题的更多详细信息,请参阅有关CSRF保护Django文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM