[英]Ajax Django Empty List
I have been stuck with this problem for a few days now and cant seem to find a solution. 我已经被这个问题困扰了几天,似乎无法找到解决方案。 I have had a look at quite a few similar questions on Google and here, but cant seem to get this working.
我在Google和这里都看过很多类似的问题,但似乎无法解决这个问题。 The closest i have come is with what i have now.
我最近来的就是我现在所拥有的。
Basically what i am trying to do is, once the user clicks on Checkout, a javascript prompt opens up and asks for a reason for checking out the item, then another straight after, asking for the name of the person who authorized the checkout. 基本上,我想做的是,一旦用户单击“结帐”,就会出现一个JavaScript提示,询问您要检查该商品的原因,然后再询问另一个人,询问授权结帐的人的名字。
<script>
$(document).ready(function(){
$('.chkrclass1').click(function(event){
var authby;
var reason;
var chkr = $('.tdata:eq(3)').html();
if (chkr == "Out Stock") {
alert("This item has already been checked out!");
event.preventDefault();
}
if (confirm("Are you sure you wish to checkout this item?") == true) {
reason = prompt("Please enter your reason for taking the item.");
authby = prompt("Please enter who authorized the checkout.");
$.ajax({
url: '/book/checkout/',
type: 'POST',
data: {"ids": authby, "idss": reason}
traditional: true,
success: function(data){
alert('Checkout Success!');
//$("body").append(data);
}
});
if (reason == '') {
alert("You never entered a reason. Cancelling checkout...");
event.preventDefault();
}
if (authby == '') {
alert("You have not entered who authorized the checkout. Cancelling checkout...");
event.preventDefault();
}
} else {
event.preventDefault();
}
});
});
</script>
I know the code is very simple and could be optimized in alot of ways, but i am still learning and will hopefully get there soon. 我知道代码很简单,并且可以通过多种方式进行优化,但是我仍在学习,希望很快就会实现。
In the view i have 我认为
def bookcheckout(request):
chkoutcons = {}
bchkoutcons['ogphead'] = "OGPSS Inventory Database"
bchkoutcons['title'] = "Checkout Success"
bchkoutcons['thedate'] = datetime.datetime.now().strftime('%d-%m-%Y')
username = request.user.username
username = username.capitalize()
if username.endswith('ogpss'):
username = username[:-5]
bchkoutcons['username'] = username
auths = request.POST.getlist('ids[0]')
reasons = request.POST.getlist('idss[0]')
bookch = BookCheckout(bookcheckoutdate=datetime.datetime.today(), bookcheckoutby=request.user.username, bookauthby=auths, bookreason=reasons, bookid=bookitem)
bookch.save()
return render_to_response('checkoutsuccess.html', bchkoutcons, context_instance=RequestContext(request))
When i go into the admin section and have a look at the info from the checkout, the values are just empty lists. 当我进入管理部分并查看结帐信息时,这些值只是空列表。
I have also tried 我也尝试过
request.POST("ids")
but get QueryDict is not callable. 但是get QueryDict是不可调用的。 As i said, still so much to learn.
正如我所说,还有很多东西要学习。
Could anyone please help with what i am doing wrong? 谁能帮我解决我的错?
POST是一本字典。
request.POST["ids"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.