Search code examples
djangodjango-viewsdjango-formsdjango-templates

Button troubleshooting in Django


By pressing the button, the sale is over and the highest price is announced as the winner. Here, the button performs the function of the button defined at the beginning of the template file.

(It doesn't do what I explained. It does the work of another button)

(Information about the previous button:A form is created using form.py and sent to the html file with the name of the form in the page_product function.Using this form, the user can add or remove the product to the saved list by pressing the button.I cannot send because of the increase in the number of codes)

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>

Solution

  • Be aware Below code is 5% pseudo code. That's because I can't see your models.py, urls.py.

    Your urls.py should look like this.

    #add
        path('bid/add/<int:product_id>',views.Bid_add, name="Bid_add"),
    
    #remove
        path('bid/remove/<int:bidid>',views.Bid_remove, name="Bid_remove"),
    
    #close
        path('bid/close/<int:bidid>',views.Bid_close, name="Bid_close"),
    

    Your views.py should look like this.

    def Bid_add(request, product_id):
        product = get_object_or_404(Product, pk=product_id)
        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', product_id)
    
    
    
    
    
    
    
    def Bid_remove(request, bidid):
        product = get_object_or_404(Product, pk=bidid)
        if request.method == 'POST':
        user = request.user
        if user.is_authenticated:
            Bid_ = Bid_info(product=product, seller=request.user)
            Bid_.remove()
                 
        return redirect('page_product', bidid)
    
    
    
    
    
    
    def Bid_close(request, productid):
    
    # CLOSE THE BID
        return render(request, 'auctions/page_product.html', context)
    
    
    
    
    def page_product(request, productid):
    
        return render(request, 'auctions/page_product.html', context)
    
    
    

    Your forms should look like this.

    
    
    <form method="post" action="{% if product.active_bool %}bid/remove/{{ bid.id }}{% else %}bid/add/{{ product.id }}{% endif %}">
        {% 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="bid/close/{{ off.id }}">
        {% csrf_token %}
        {% if close_button %}
            <button type="submit" class="btn btn-primary">Close</button> 
        {% else %}
            <p> {{off.seller}} is seller!</p>
        {% endif %}
    </form>