按下按钮,销售结束,最高价格被宣布为获胜者。 这里,按钮执行模板文件开头定义的按钮的功能。
(它不执行我解释的操作。它执行另一个按钮的工作)
(有关上一个按钮的信息:使用 form.py 创建一个表单,并将其发送到 html 文件,并在 page_product 函数中使用该表单的名称。使用此表单,用户可以通过以下方式将产品添加或删除到保存的列表中:按下按钮。因为代码数量增加而无法发送)
views.py:
def page_product(request, productid):
product = get_object_or_404(Product, pk=productid)
off = Bid_info.objects.get(product=product)
bid_max = Bid_info.objects.filter(product_id=productid).latest('bid_price')
if request.user == off.seller:
close_button = True
off.bid_price = bid_max
Bid_info.checkclose = True
messages.success(request, f"The offer was closed at {bid_max} $")
else:
close_button = False
context = {
'product' : product,
'form': form,
'off' : off,
'close_button' : close_button,
'bid_max' : bid_max
}
return render(request, 'auctions/page_product.html', context)
def bid(request, bidid):
product = get_object_or_404(Product, pk=bidid)
if request.method == 'POST':
user = request.user
if user.is_authenticated:
bid_price = Decimal(request.POST.get("bid_price", False))
other_off = Bid_info.objects.filter(product=product).order_by("-bid_price").first()
if Bid_info.checkclose == True:
messages.warning(request, "it sells.")
else:
Bid_ = Bid_info(product=product, seller=request.user, bid_price=bid_price)
Bid_.save()
return redirect('page_product', bidid)
page_product.html:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="rectangle-button">
{% if product.active_bool %}
<div style="background-color: red;"> remove</div>
{% else %}
<div style="background-color: green;">add</div>
{% endif %}
</button>
</form>
<form method="post">
{% csrf_token %}
{% if close_button %}
<button type="submit" class="btn btn-primary">Close</button>
{% else %}
<p> {{off.seller}} is seller!</p>
{% endif %}
</form>
无需在同一页面(模板)内使用多个表单定义不同的操作网址,所有表单都会发布到同一网址,即放置页面网址表单。
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="rectangle-button">
{% if product.active_bool %}
<div style="background-color: red;"> remove</div>
{% else %}
<div style="background-color: green;">add</div>
{% endif %}
</button>
</form>
<form method="post">
{% csrf_token %}
{% if close_button %}
<button type="submit" class="btn btn-primary">Close</button>
{% else %}
<p> {{off.seller}} is seller!</p>
{% endif %}
</form>
在上面的两个表单中,定义不同的操作 URL。 就像下面这样。
<form method="post" action="form_1_process">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="rectangle-button">
{% if product.active_bool %}
<div style="background-color: red;"> remove</div>
{% else %}
<div style="background-color: green;">add</div>
{% endif %}
</button>
</form>
<form method="post" action="form_2_process">
{% csrf_token %}
{% if close_button %}
<button type="submit" class="btn btn-primary">Close</button>
{% else %}
<p> {{off.seller}} is seller!</p>
{% endif %}
</form>
这些
action="form_1_process">
使表单提交网址如下。
Page_url+"/"+"form_1_process"
意味着,表单被发布到此网址。