我已经被这个问题困扰了几天,似乎无法找到解决方案。我在Google和这里都看过很多类似的问题,但似乎无法解决这个问题。我最近来的就是我现在所拥有的。

基本上,我想做的是,一旦用户单击“结帐”,就会打开一个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>


我知道代码很简单,并且可以通过多种方式进行优化,但是我仍在学习,希望很快就会实现。

我认为

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))


当我进入管理部分并查看结帐信息时,这些值只是空列表。

我也尝试过

request.POST("ids")


但是get QueryDict是不可调用的。正如我所说,还有很多东西要学习。

谁能帮我解决我的错?

最佳答案

POST是一本字典。

request.POST["ids"]

关于javascript - Ajax Django空列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24951335/

10-12 13:03