简体   繁体   English

禁止(403)CSRF验证失败。 请求使用django中止

[英]Forbidden (403) CSRF verification failed. Request aborted using django

hello i am user python and Django. 你好我是用户python和Django。

i will follow this question(Can I have a Django form without Model) but for my task i need images. 我将遵循这个问题(我可以在没有模型的情况下使用Django表格)但是对于我的任务我需要图像。 i will try to run my code and i have this error Forbidden (403) : any idea how to fix that ? 我将尝试运行我的代码,我有这个错误禁止(403):任何想法如何解决这个问题?

Forbidden (403)
CSRF verification failed. Request aborted.
You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.
If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for 'same-origin' requests.

views.py views.py

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
from blog.forms import MyForm

# Create your views here.
def form_handle(request):
    form = MyForm()
    if request.method=='POST':
        form = MyForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            #now in the object cd, you have the form as a dictionary.
            a = cd.get('a')
    return render_to_response('blog/calc.html', {'form': form}, RequestContext(request))

urls.py urls.py

from django.conf.urls import url
from . import views

forms.py forms.py

from django import forms

class MyForm(forms.Form): #Note that it is not inheriting from forms.ModelForm
    a = forms.ImageField()
    #All my attributes here

urls.py urls.py

urlpatterns = [
url(r'^$',views.form_handle, name='form_handle'),

]

html HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="" method="POST">{% csrf_token %}
    {{form.as_p}}
    <button type="submit">Submit</button>
</form>
</body>
</html>

You are adding a csrf token to the form, but not providing it in the view function. 您正在向表单添加csrf标记,但不在视图函数中提供它。 Try this: 尝试这个:

from django.views.decorators.csrf import csrf_protect

# Create your views here.
@csrf_protect
def form_handle(request):
    form = MyForm()
    ...

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

相关问题 禁止(403)CSRF验证失败。 请求中止。 Django的 - Forbidden (403) CSRF verification failed. Request aborted. Django 禁止(403)CSRF验证失败。 请求在Django 1.8.5中中止2 - Forbidden (403) CSRF verification failed. Request aborted 2 in django 1.8.5 禁止(403)CSRF验证失败。 请求在Django 1.11上中止 - Forbidden (403) CSRF verification failed. Request aborted on Django 1.11 禁止(403)CSRF验证失败。 请求中止。在Django中 - Forbidden (403) CSRF verification failed. Request aborted.in Django 禁止(403)CSRF验证失败。 请求中止 - Forbidden (403) CSRF verification failed. Request aborted 禁止(403)CSRF验证失败。 请求中止 - Forbidden (403) CSRF verification failed. Request aborted 禁止(403)CSRF验证失败。 请求中止。 即使使用{%csrf_token%} - Forbidden (403) CSRF verification failed. Request aborted. Even using the {% csrf_token %} 禁止Django注册(403)CSRF验证失败。 请求中止 - Django-registration Forbidden (403) CSRF verification failed. Request aborted 如何解决“禁止(403)CSRF验证失败。 请求中止。” Django中的错误 - How to fix “Forbidden (403) CSRF verification failed. Request aborted.” error in django 禁止 (403) CSRF 验证失败。 请求中止 - 与 Django 频道的实时聊天应用程序 - Forbidden (403) CSRF verification failed. Request aborted-Real time chat application with Django Channels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM