[英]CSRF protection on Django FormView
例如,我在views.py中使用它:
@csrf_protect
def contacts(request):
pass
現在我想使用FormView :
class ContactFormView(FormView):
template_name = 'contacts.html'
form_class = ContactForm
success_url = '/'
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
form.send_email()
return super(ContactFormView, self).form_valid(form)
所以,我需要在哪里使用@csrf_protect裝飾器?
謝謝!
您應該在dispatch
方法上使用method_decorator
:
from django.utils.decorators import method_decorator
class ContactFormView(FormView):
...
@method_decorator(csrf_protect)
def dispatch(self, *args, **kwargs):
return super(ContactFormView, self).dispatch(*args, **kwargs)
但是,強烈建議改用CsrfViewMiddleware
。 否則,您偶然忘記裝飾器的單個實例將立即帶來安全風險。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.