gpt4 book ai didi

python - CSRF cookie 未设置 django...验证失败

转载 作者:太空狗 更新时间:2023-10-30 02:04:35 27 4
gpt4 key购买 nike

迎角 我是 Django 的新手,我正在尝试从 POST 获取数据,但未设置错误 CSRF cookie,我也尝试通过谷歌在谷歌和 stackoverflow 上找到解决方案,但失败了

这是代码

View .py

    from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context
from django.template import RequestContext
from django.core.context_processors import csrf
from django.shortcuts import render_to_response

def search_Post(request):
if request.method == 'POST':
c = {}
c.update(csrf(request))
# ... view code here
return render_to_response("search.html", c)

def search_Page(request):
name='Awais you have visited my website :P'
t = get_template('search.html')
html = t.render(Context({'name':name}))
return HttpResponse(html)

HTML 文件

<p>
{{ name }}
<form method="POST" action="/save/">
{% csrf_token %}
<textarea name="content" rows="20" cols="60">{{content}}</textarea><br>
<input type="submit" value="Save Page"/>
</form>
<div> Cant figure out any solution! :( </div>

</p>

网址.py

 url(r'^home/$', 'contacts.views.home_Page'),
url(r'^save/$', 'contacts.views.search_Post'),
url(r'^edit/$', 'contacts.views.edit_Page'),
url(r'^search/$', 'contacts.views.search_Page'),

设置.py

TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.csrf',
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.request',
'django.contrib.messages.context_processors.messages'
)

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

最佳答案

我有同样的问题,并通过添加 ensure_csrf_cookie decorator 解决了它您的看法:

 from django.views.decorators.csrf import ensure_csrf_cookie
@ensure_csrf_cookie
def yourView(request):
#...

它会在浏览器cookie中设置csrftoken,你可以像这样制作ajax

function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
crossDomain: false, // obviates need for sameOrigin test
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
}
});
$.ajax({
url: url,
type: type,
async: async,
data: data,
error: function (e) {},
success: function (data) {
returnFunction(data);
}
});

关于python - CSRF cookie 未设置 django...验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19598993/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com