问题描述
我开始在Django中使用插件,并迅速遇到CSRF错误: CSRF验证失败。请求中止。, CSRF令牌丢失或不正确
I started using the Jeditable plugin with Django and quickly ran into a CSRF error: "CSRF verification failed. Request aborted.", "CSRF token missing or incorrect"
在撰写本文时,Jeditable插件似乎最后一次更新是在2008年-在此Django之后的某个时间开始对POST请求要求。
As of this writing the Jeditable plugin seems have been last updated in 2008--sometime after this Django began requiring CSRF tokens for POST requests.
如何将Django CSRF数据添加到Jeditable中?
How do you add Django CSRF data to Jeditable?
推荐答案
这个问题的答案来了来自。可以将CSRF令牌添加到 submitdata变量中。
The answer to this question came from a similar jeditable post on CSRF. The CSRF token may be added in the "submitdata" variable.
扩展类似于Django帖子:
Expanding the 1st jeditable example for a Django post look something like this:
$(document).ready(function() {
$('.edit').editable('http://www.example.com/save.php', {
submitdata : { csrfmiddlewaretoken : "{{ csrf_token }}"}
});
});
注意,为了显示 csrf_token值而不是整个表单字段,请显示 csrf_token 包裹在{{..}}中,而不是{%..%}中。
Note, in order to show the "csrf_token" value instead of an entire form field, the "csrf_token" is wrapped in {{..}} instead of {% .. %}.
这篇关于Django和JEditable:CSRF错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!