Greg Strydom
Greg Strydom

Reputation: 159

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

<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. As i said, still so much to learn.

Could anyone please help with what i am doing wrong?

Upvotes: 1

Views: 164

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599866

POST is a dictionary.

request.POST["ids"]

Upvotes: 2

Related Questions