无法查询“zs”:必须是“Product”实例

Cannot query "zs": Must be "Product" instance

提问人:zahra shokrizadeh 提问时间:9/25/2023 最后编辑:zahra shokrizadeh 更新时间:9/25/2023 访问量:42

问:

我希望卖家能够选择其中一个待售报价,然后通过单击关闭按钮关闭销售。 如果我在 html 文件中放置一个单独的 for 循环,此代码不会执行任何操作,但如果这是我发送它的方式,它会显示关闭销售的按钮,但在单击按钮后,它会给出此错误。

*zs 是用户名。此外,我希望每个用户只能提出一次报价。

而且,我觉得这个图形不是很人性化。对于更好的设计和减少关闭拍卖的按钮数量,您有什么建议?page view

Error:

Exception Type: ValueError at /seller_page/


Exception Value: Cannot query "zs": Must be "Product" instance.

views.py:

def seller_page(request):
    seller_offers = PriceOffer.objects.filter(product=request.user)
    if request.method == 'POST':
        offer_id = request.POST.get('offer_id')
        offer = get_object_or_404(PriceOffer, pk=offer_id)
        offer.is_closed = True
        offer.save()
    return render(request, 'auctions/product_detail.html', {'seller_offers': seller_offers})


def close_offer(request): 
    if request.method == 'POST': 
        offer_id = request.POST.get('offer_id') 
        offer = get_object_or_404(PriceOffer, pk=offer_id)
        offer.is_closed = True 
        offer.save() 
    return redirect('seller_page')

product_detail.html :

<ul>
     {% for offer in offers %}
          <li><a> {{ offer.author }}:{{ offer.offer_price }}</a></li>
            
          <form method="post" action="{% url 'close_offer' %}">
              {% csrf_token %}
              <input type="hidden" name="offer_id" value="{{ offer.id }}">
              <button type="submit">Close Offer</button>
          </form>
            
     {% endfor %}

</ul>
    <form method="POST" action="{%url 'product_detail' product.id %}">
        {% csrf_token %}
        <input type="number" name="offer_price" placeholder="Offer Price">
        <button type="submit" name="offer">Submit Offer</button>
    </form>

models.py:

class PriceOffer(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    seller = models.ForeignKey(User, on_delete=models.CASCADE)
    author = models.CharField(max_length=100)
    offer_price = models.DecimalField(max_digits=10, decimal_places=2)
    created_at = models.DateTimeField(auto_now_add=True)
    is_closed = models.BooleanField(default=False)

    def __str__(self):
        return f'Price offer by {self.seller} on {self.product}: {self.offer_price}`

urls.py:

path('seller_page/', views.seller_page, name='seller_page'), 
path('close_offer/', views.close_offer, name='close_offer'),
python html django 模型

评论


答: 暂无答案