Over the last week or so I have been struggling to successfully implement a jQuery ajax request to pass data from a javascript function within my annualgoals.html to a django view in order to save data to my database. Currently I am just trying to test the data transfer by printing the data on the django end.
在过去的一周左右,我一直在努力成功地实现jQuery ajax请求,以便在我的年度目标中从javascript函数中传递数据。将html保存到django视图,以便将数据保存到数据库。目前我正在尝试通过在django端上打印数据来测试数据传输。
I have already checked out the following threads but cannot see any that show the same error as I can see (I am almost certainly missing something obvious).
我已经检查了下面的线程,但是没有看到任何线程显示与我看到的相同的错误(我几乎肯定漏掉了一些明显的错误)。
Forbidden (CSRF token missing or incorrect) Django error
禁止(CSRF令牌丢失或不正确)Django错误
"CSRF token missing or incorrect" while post parameter via AJAX in Django
“CSRF令牌丢失或不正确”,同时通过AJAX在Django中发布参数
While a missing CSRF token was my first problem, the answers on * and the django docs do a good job of explaining what must be done to ensure the csrf token is included. However, when following the recommended steps I receive the following django error:
虽然丢失的CSRF令牌是我的第一个问题,但是*和django文档上的答案很好地解释了必须做什么才能确保包含CSRF令牌。但是,当按照推荐的步骤执行时,我收到以下django错误:
response.set_cookie(settings.CSRF_COOKIE_NAME, AttributeError: 'NoneType' object has no attribute 'set_cookie'
response.set_cookie(设置。CSRF_COOKIE_NAME, AttributeError: 'NoneType'对象没有属性'set_cookie'
My code is as follows:
我的代码如下:
annualgoals.html
annualgoals.html
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));
}
var data = [1, 2, 3, 4]
var csrftoken = getCookie('csrftoken');
// Ajax Setup
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
// Ajax Call
$.ajax({
type: 'POST',
url: "{% url 'LifeAdmin:saveAnnualGoals' %}",
data: {'data[]': data},
});
views.py
views.py
@ensure_csrf_cookie
@login_required
def saveAnnualGoals(request):
if request.method == 'POST':
data = request.POST.get('data')
print(data)
urls.py
urls . py
url(r'^saveAnnualGoals$', views.saveAnnualGoals, name='saveAnnualGoals')
I am using Django 1.10.5 and Python 3.6.1
我使用的是Django 1.10.5和Python 3.6.1
Any help would be greatly appreciated!!
非常感谢您的帮助!!
Thanks :)
谢谢:)
1 个解决方案
#1
3
Your view needs to return a Response. With your actual code, the view returns nothing, so None
. Django then tries to add the cookie to the Response but as it is None, you get the error message you posted.
您的视图需要返回一个响应。对于实际的代码,视图不返回任何东西,所以没有。Django尝试将cookie添加到响应中,但是因为它不是None,所以您将获得您所发布的错误消息。
Example:
例子:
return HttpResponse(status=200)
#1
3
Your view needs to return a Response. With your actual code, the view returns nothing, so None
. Django then tries to add the cookie to the Response but as it is None, you get the error message you posted.
您的视图需要返回一个响应。对于实际的代码,视图不返回任何东西,所以没有。Django尝试将cookie添加到响应中,但是因为它不是None,所以您将获得您所发布的错误消息。
Example:
例子:
return HttpResponse(status=200)