Почему будет работать только верхняя форма (по порядку в представлении Django), а та, что под ней - нет?
У меня есть Django view с двумя Django формами. Обе они ранее работали. Теперь работает та, которая первая по порядку кода (сверху). Я пробовал поместить обе формы друг на друга, и та, которая первая, работает, но вторая ничего не делает, когда ее отправляют. Кто-нибудь знает, в чем проблема и как это исправить?
формы в файле views.py
def listing(request, id):
#gets listing
listing = get_object_or_404(Listings.objects, pk=id)
#code for comment and bid forms
listing_price = listing.bid
sellar = listing.user
comment_obj = Comments.objects.filter(listing=listing)
#types of forms
comment_form = CommentForm()
bid_form = BidsForm()
#code for the bid form
bid_obj = Bids.objects.filter(listing=listing)
other_bids = bid_obj.all()
max_bid =0
for bid in other_bids:
if bid.bid > max_bid:
max_bid = bid.bid
if request.method == "POST":
#forms
comment_form = CommentForm(request.POST)
bid_form = BidsForm(request.POST)
#checks if bid form is valid
if bid_form.is_valid():
print('!!!!!form is valid')
#print("bid form is valid")
print(listing.bid)
new_bid = float(bid_form.cleaned_data.get("bid"))
if (new_bid >= listing_price) and (new_bid > max_bid):
bid = bid_form.save(commit=False)
bid.listing = listing
bid.user = request.user
bid.save()
print("bid form is saving")
else:
print(bid_form.errors)
print("bid form is not saving")
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": comment_form,
"comments": comment_obj,
"bidForm": bid_form,
"bids": bid_obj,
"message": "Your bid needs to be equal or greater than the listing price and greater than any other bids."
})
else:
print(bid_form.errors, bid_form.non_field_errors)
print(bid_form.errors)
return redirect('listing', id=id)
#checks if comment form is valid
if comment_form.is_valid():
print("comment is valid")
comment = comment_form.save(commit=False)
comment.listing = listing
comment.user = request.user
comment.save()
else:
return redirect('listing', id=id)
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": comment_form,
"comments": comment_obj,
"bidForm": bid_form,
"bids": bid_obj
})