Non-separation of elements

With this code, after pressing the close button, not only that product but all products will be sold, and the buyer cannot offer a price for any of them.

views.py:

@login_required
def page_product(request, productid):
    product =get_object_or_404(Product, pk=productid)
    offers = Bid_info.objects.filter(product=product).order_by("-bid_price")
    off = Bid_info.objects.filter(product=product).order_by('id').first()


    # max bid   
    try:
        bid_max = Bid_info.objects.filter(product_id=productid).latest('bid_price')
    except:
        bid_max ="No offer has been registered yet!"
    
    # close
    close_button = False
    try:
        if request.user == product.user:
                close_button = True
                off.bid_price = bid_max
                win = 
        Bid_info.objects.filter(product_id=productid).order_by('id').latest("seller")
            
                for item in Bid_info.product:
                    if item.id == Bid_info.product.id:
                        Bid_info.checkclose = not Bid_info.checkclose
        else:
            close_button = False
    except:
        pass
    

context = {
    'product' : product,,
    'offers' : offers,
    'off' : off,
    'close_button' : close_button, 
    'bid_max' : bid_max,
    'checkclose' : Bid_info.checkclose,
    'winner' : win   
}
return render(request, 'auctions/page_product.html', context)


def close(request, cid):
    return redirect(page_product, cid


# bid
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 :
                messages.warning(request, "it sells.")
        
            else:
                Bid_ = Bid_info(product=product, seller=request.user, bid_price=bid_price)
            
    return redirect('page_product', bidid)

If I delete the for loop in the first function and write this in the bid function in the if loop:
if Bid_info.checkclose== true
This is the situation again.

html:

<form method="post"  action="{% url 'close' product.id %}">
    {% csrf_token %}
    {% if close_button %}
        <button type="submit" class="btn btn-primary">Close</button> 
    {% else %}
        <p> {{product.user}} is seller!</p>
    {% endif %}
</form>

models.py:

class Bid_info(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name="seller")
    bid_price = models.DecimalField(max_digits=10, decimal_places=2)
    checkclose = models.BooleanField(default=False)
    winner = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, 
                                                               blank=True)

  • Well the question is what Bid_info is, at first point it seems to be a bid of someone, but if that is the case, why is there a winner field? Likely you have a Product, an Offer that should be an offer for an instance of that product, and then a Bid that refers to the Offer.

    – 

  • @willeM_VanOnsem I don’t understand what you mean, but Bid_info is a model for sale. And I have a model for creating and product information called Product. The sales model gives information about the offer, and finally only one person wins, and that is why the winner field is there.

    – 

  • but for the same product there are thus multiple bid_infos? Then these all have the same winner?

    – 

  • @willeM_VanOnsem Well, doesn’t every product have its own information? That is, the winner field should be in the Product model? Is it ok? Or should a separate model be defined?

    – 

  • 1

    your question is vague and your code block also have indentation problem. try to ask a clear question first. the best way to find the problem is, start debugging your code (maybe by the help of your IDE), or at least try to log/print the state of some variables (even in html) to examine your logic. the logic inside the for loop is weird!

    – 

Leave a Comment