Формы Django принимают заданное значение кнопки submit, а не ввод пользователя
У меня есть две формы Django на одной странице, я разделяю их следующим образом:
if request.method == "POST" and "bid" in request.POST:
form1 = NewBidForm(request.POST)
if form1.is_valid():
# Makes sure the big is higher than the previous highest bid
if float(request.POST["bid"]) > listing.startingbid:
# Saves bidding to database
bidding = form1.cleaned_data["bid"]
newbid = Bid(bid=bidding, user=request.user, listing=listing)
newbid.save()
listing.startingbid = bidding
listing.save()
return HttpResponseRedirect(reverse("listing", args=[name]))
else:
messages.error(request, 'This bid is not higher than the previous bid, try again!')
return HttpResponseRedirect(reverse("listing", args=[name]))
# If the form is not valid, return user to the original listing page
else:
return render(request, "auctions/listing.html", {
"listing": listing,
"comments": comments,
"bids": bids,
"highestbid": highestbid,
"bidform": NewBidForm(),
"commentform": NewCommentForm()
})
# Handles user input for user placing a comment on a listing
elif request.method == "POST" and "comment" in request.POST:
form2 = NewCommentForm(request.POST)
print(form2)
if form2.is_valid():
# Saves comment to database
comment = form2.cleaned_data["comment"]
newcomment = Comment(comment=comment, user=request.user, listing=listing)
newcomment.save()
return HttpResponseRedirect(reverse("listing", args=[name]))
else:
return render(request, "auctions/listing.html", {
"listing": listing,
"comments": comments,
"bids": bids,
"highestbid": highestbid,
"bidform": NewBidForm(),
"commentform": NewCommentForm()
})
Формы выглядят следующим образом:
class NewBidForm(forms.Form):
bid = forms.DecimalField(label="bid", decimal_places=2, max_digits=10)
class NewCommentForm(forms.Form):
comment = forms.CharField(label="comment", widget=forms.Textarea)
и мой HTML выглядит следующим образом:
<form action="{% url 'listing' listing.title %}" method="POST">
{% csrf_token %}
{{ bidform }}
<input type="submit", name="bid", value="Place Bid">
</form>
<form action="{% url 'listing' listing.title %}" method="POST">
{% csrf_token %}
{{ commentform }}
<input type="submit", name="comment", value="Place Comment">
</form>
Однако проблема в том, что значение, которое я получаю из обеих форм, является значением на кнопках <input>
, соответственно "Place Bid" и "Place Comment". Почему так происходит и как мне получить фактический ввод пользователя из формы?
Любая помощь будет высоко оценена, спасибо! Если нужна дополнительная информация, пожалуйста, прокомментируйте и дайте мне знать!