我在Django中创建了一个简单的表单,而不使用内置的Django表单。为了处理表单,我只是在views.py文件中使用HTML/Django模板和Python。
我不明白为什么当print(bid > highest_bid)返回True时:
最高出价= 2000.6
出价=3
listing_detail.html
<form method="POST" action="{% url 'listing-detail' object.id %}">
{% csrf_token %}
<input type="hidden" name="highest_bid" value="{{ highest_bid }}">
<input type="number" step=".01" min="" placeholder="0.00" name="bid">
<button type="submit">Place bid</button>
</form>views.py
# place bid
highest_bid = request.POST.get("highest_bid", "") #2000.6
print(f'Highest = {highest_bid}')
bid = request.POST.get("bid", "") #3
print(f'bid = {bid}')
print(bid > highest_bid) # returns True
if 'bid' in request.POST and bid > highest_bid:
#bid = float(request.POST['bid'])
new_bid = Bids(bid_value=bid, bidder=self.request.user, item=item)
new_bid.save()
return redirect(reverse("listing-detail", args=listing_id))这意味着投标将保存到数据库中。即使我只想用这条线保存最高的出价
if 'bid' in request.POST and bid > highest_bid:发布于 2021-10-02 08:57:21
问题是,您正在比较字符串,而不是number.to,避免这种情况,您应该像这样转换变量。
print(int(bid) > int(highest_bid)) or print(float(bid) > float(highest_bid))试着改变这个
if 'bid' in request.POST and bid > highest_bid:至
if 'bid' in request.POST and float(bid) > float(highest_bid):https://stackoverflow.com/questions/69415359
复制相似问题