如何访问Django表格的提交值?
我正在尝试建立一个电子商务网站,以使用户可以通过表格订立出价。用户帖子必须大于上市价格和任何其他出价的新出价。我需要帮助访问用户提交的价值以检查它是否符合先前所指定的要求。
views.py
def listing(request, id):
#gets listing
listing = Listings.objects.get(id=id)
#code for forms
listing_price = listing.bid
comment_obj = Comments.objects.filter(listing=listing)
form = CommentForm()
bid_form = BidsForm()
if request.method == "POST":
form = CommentForm(request.POST)
bid_form = BidsForm(request.POST)
new_bid = bid_form.cleaned_data.get('newBid')
if form.is_valid():
comment = form.save(commit=False)
comment.listing = listing
comment.user = request.user
comment.save()
if (bid_form.is_valid()) and (new_bid >= listing_price):
bid = form.save(commit=False)
bid.listing = listing
bid.user = request.user
bid.save()
else:
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": comment_obj,
"bidForm": bid_form
})
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": comment_obj,
"bidForm": bid_form
})
(有两种形式,一种用于评论,另一种用于投标。)
html
<!--bid form-->
<form action = "{% url 'listing' auction_listing.id %}" method = "POST" name = "newBid">
{% csrf_token %}
{{ bidForm }}
<input type = "submit" value = "Place Bid">
</form>
I am trying to make an e-commerce site that allows the user to place bids through a form. The new bid that the user posts has to be larger than the listing price on the listing and any other bids. I need help accessing the value that the user submits to check if it meets the previously stated requirements.
views.py
def listing(request, id):
#gets listing
listing = Listings.objects.get(id=id)
#code for forms
listing_price = listing.bid
comment_obj = Comments.objects.filter(listing=listing)
form = CommentForm()
bid_form = BidsForm()
if request.method == "POST":
form = CommentForm(request.POST)
bid_form = BidsForm(request.POST)
new_bid = bid_form.cleaned_data.get('newBid')
if form.is_valid():
comment = form.save(commit=False)
comment.listing = listing
comment.user = request.user
comment.save()
if (bid_form.is_valid()) and (new_bid >= listing_price):
bid = form.save(commit=False)
bid.listing = listing
bid.user = request.user
bid.save()
else:
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": comment_obj,
"bidForm": bid_form
})
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": comment_obj,
"bidForm": bid_form
})
(There are two forms, one for comments and one for bids.)
html
<!--bid form-->
<form action = "{% url 'listing' auction_listing.id %}" method = "POST" name = "newBid">
{% csrf_token %}
{{ bidForm }}
<input type = "submit" value = "Place Bid">
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过添加以下代码来访问上市价格的价值:
new_bid = bid_form.cleaned_data.get(“ bid”)
之后如果bid_form.is_valid():
:并创建了一个新if语句以检查new_bid是否比listing_price_price_price_price_price_price_price_price 。我还删除了If request.method ==“ post”:
,这引起了错误。I was able to access the value of the listing price by adding this line of code:
new_bid = bid_form.cleaned_data.get("bid")
afterif bid_form.is_valid():
and created a new if statement to check if the new_bid is larger than the listing_price. I also removedif request.method == "POST":
, which was causing an error.