[英]Ajax POST call Django's View with Multiple Data [Response 500]
將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模板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>
您的vote
功能應僅接受一個參數,即request
:
def vote(request):
通過AJAX傳遞給視圖的變量將出現在request.POST
字典中。 它們不會作為args
或kwargs
傳遞給view函數。
def vote(request):
content_type = request.POST['content_type']
vote = request.POST['vote']
object_id = request.POST['object_id']
...
有兩件事需要改變...
第一
在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);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.