如何从views.py中禁用表单?
我如何实现以下内容:
@login_required
def close_auction(request,listing_id):
listing = Listing.objects.get(pk=listing_id)
if request.method == "POST":
listing.Auction_closed = True
**Disable the Bids on the Listing And display some message such as "The auction is Closed"**
return render(request, "auctions/index.html",{
"listing": Listing.objects.get(pk=listing_id),
"user": User.objects.get(pk=request.user.id),
"owner": listing.owner
})
是我的代码 index.html :
<!-- if the user is the one who created the listing:
they can close the listing
go to the close_auction view to close
-->
{% if user == owner %}
<form action="{% url 'close_auction' listing.id %}" method="post">
{%csrf_token%}
<button>Close this Listing</button>
</form>
{% endif %}
下面是我的型号。
class Listing(models.Model):
Title = models.CharField(max_length=64)
Description = models.TextField(max_length=500)
Category = models.CharField(max_length=16)
Starting_Bid = models.IntegerField()
Image = models.ImageField()
Auction_closed = models.BooleanField(default=False)
#def bid(self):
#return self.Starting_Bid
class User(AbstractUser):
watchlist = models.ManyToManyField(Listing, blank= True, related_name="watcher")
listing_owner = models.ForeignKey(Listing,on_delete=models.CASCADE,related_name="owner",null=True)
class Bid(models.Model):
Bid_amount = models.IntegerField()
listing = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name="bids")
bid_placed_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name="bid_placer", null=True)
以下 这里:
- 如果用户是上市的所有者,则他/她应该能够“关闭清单”。
- 一旦列表关闭,应禁用“放置出价”表单,并应显示一些消息。
How do I achieve the below:
@login_required
def close_auction(request,listing_id):
listing = Listing.objects.get(pk=listing_id)
if request.method == "POST":
listing.Auction_closed = True
**Disable the Bids on the Listing And display some message such as "The auction is Closed"**
return render(request, "auctions/index.html",{
"listing": Listing.objects.get(pk=listing_id),
"user": User.objects.get(pk=request.user.id),
"owner": listing.owner
})
Below is my code in index.html:
<!-- if the user is the one who created the listing:
they can close the listing
go to the close_auction view to close
-->
{% if user == owner %}
<form action="{% url 'close_auction' listing.id %}" method="post">
{%csrf_token%}
<button>Close this Listing</button>
</form>
{% endif %}
Below is my models.py:
class Listing(models.Model):
Title = models.CharField(max_length=64)
Description = models.TextField(max_length=500)
Category = models.CharField(max_length=16)
Starting_Bid = models.IntegerField()
Image = models.ImageField()
Auction_closed = models.BooleanField(default=False)
#def bid(self):
#return self.Starting_Bid
class User(AbstractUser):
watchlist = models.ManyToManyField(Listing, blank= True, related_name="watcher")
listing_owner = models.ForeignKey(Listing,on_delete=models.CASCADE,related_name="owner",null=True)
class Bid(models.Model):
Bid_amount = models.IntegerField()
listing = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name="bids")
bid_placed_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name="bid_placer", null=True)
What I am trying to achieve here:
- If the user is the owner of the listing then he/she should be able to 'close the listing'.
- Once the listing is closed, the 'Place a Bid' form should be disabled and some message should be shown instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论