如何从views.py中禁用表单?

发布于 2025-01-25 11:44:00 字数 2078 浏览 2 评论 0原文

我如何实现以下内容:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文