Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

python - Django Error - MultiValueDictKeyError at /new_bid/5 'total_bid.' (Beginner help appreciated)

I'm creating a page on a site to allow users to 'bid' on items similar to ebay. When I try to enter a number (dollar amount) on the item on the site (to enter a bid) I get the error: MultiValueDictKeyError at /new_bid/5 'total_bid.'

The problem seems to be total_bid = request.POST["total_bid"] and I think the system believes it's None. I'm not sure why it thinks it's None because when entering the bid on the site, a number really is in the box, so if I type 100 meaning $100 bid, maybe the code is not actually taking in that number? So on POST it thinks there is nothing there?

I also tried total_bid = request.POST.get["total_bid"] with the .get part and also received an error.

I also tried adding something like "if total_bid is not none:" but I'm not sure how to do that with the syntax because I would need to declare total_bid = to something first.

I'm new to Django so additional information on how this works would be so helpful. I've already read a lot of posts on this site about this and similar errors but I'm having some trouble understanding.

views.py

def new_bid(request, listingid):
    if request.method == 'POST':
        auction_to_add = Listings.objects.get(id=listingid)
        total_bid = request.POST["total_bid"]
        bid = Bid.objects.create(user=request.user, listingid=auction_to_add, bid=total_bid)
        auction_to_add.bids.add(bid)
        auction_to_add.last_bid = bid
        auction_to_add.save()
        return render(request, "auctions/listing.html", {
            "id": listingid,
        })
    else:
        return render(request, "auctions/listing.html", {
            "id": listingid,
        })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The correct answer, which you've obviously looked at can be found here and it is as you said by using .get which does not raise an Exception.

What you are doing wrong at request.POST.get["total_bid"] is you are using [] instead of () which is invalid. You need to use request.POST.get("total_bid") because get is a method not a dictionary (or anything that can be accessed in a similar way).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...