将django-secretballot与我的应用程序“flavor”集成后,此500状态错误不是CSRFtoken问题。 FireBug网络选项卡显示
POST:
content_type flavor.Flavor
object_id 5
vote 1
Source
content_type=flavor.Flavor&object_id=5&vote=1
响应:
TypeError at /flavor/vote/
vote() missing 3 required positional arguments: 'content_type', 'object_id', and 'vote'
你能发现我的flavor_vote.js中的任何错误吗?为什么不能将这3个必需的位置参数传递给vote()函数?
$(document).ready(function() {
var csrftoken = getCookie('csrftoken');
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
},
error: function(xhr, textStatus, error) {
console.log(error);
}
});
$(".vote").click(function () {
var div = $(this);
var obj_id = $(this).closest('.flavor').attr('flavor-id');
var vote = 1;
if ($(this).hasClass('voted')) {
var vote = 0;
}
console.log(div);
$.ajax({
url: '/flavor/vote/',
method: 'post',
data: {
'content_type': 'flavor.flavor',
'object_id': obj_id,
'vote': vote
},
success: function (data) {
alert('Successfully called');
$(div).toggleClass('btn-success voted');
console.log(data);
},
error: function(data) {
console.log(data);
}
});
});
});
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));
}
========其他相关信息================
我的models.py有Flavor类,可以投票
secretballot.enable_voting_on(Story)
我的ajax调用需要将参数传递给https://github.com/jamesturk/django-secretballot/blob/master/secretballot/views.py中的views.py中的vote()
函数
def vote(request, content_type, object_id, vote, can_vote_test=None,
redirect_url=None, template_name=None, template_loader=loader,
extra_context=None, context_processors=None, mimetype=None):
urls.py有
url(r'^flavor/vote/$', 'secretballot.views.vote', name='flavor_vote'),
django template flavor_detail.html
<div class="caption flavor" flavor-id="{{ flavor.id }}">
<div class="btn btn-default vote">
<span class="glyphicon glyphicon-thumbs-up"></span>
Vote
</div>
</div>
答案 0 :(得分:0)
您的vote
函数应该只使用一个参数request
:
def vote(request):
通过AJAX传递给视图的变量将出现在request.POST
dict中。它们不会作为args
或kwargs
传递给视图函数。
def vote(request):
content_type = request.POST['content_type']
vote = request.POST['vote']
object_id = request.POST['object_id']
...
答案 1 :(得分:0)
需要改变两件事......
第一
在urls.py中添加对位置参数的支持
url(r'^flavor/vote/(?P<content_type>)/(?P<object_id>)/(?P<vote>)', 'secretballot.views.vote', name='flavor_vote'),
第二
像这样更改你的ajax请求。
type = "some type"; // fetch it somehow.
$.ajax({
url: '/flavor/vote/'+type+'/'+obj_id+'/'+ vote,
method: 'post',
data: { //not needed now as the url contains it.
'content_type': 'flavor.flavor',
'object_id': obj_id,
'vote': vote
},
success: function (data) {
alert('Successfully called');
$(div).toggleClass('btn-success voted');
console.log(data);
},
error: function(data) {
console.log(data);
}
});