how to load the sale price based on the selecter with django

  Kiến thức lập trình

hi I am a beginner on django and I would like to display the sale price depending on the product selected, see my source codes below. I’m stuck and I can’t unblock, please I need your help.
Thanks for your help

the sales model:

class Sale(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    product = models.ForeignKey('Product', on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(default=1)
    sale_price = models.PositiveIntegerField(default=0)
    montant_total = models.CharField(max_length=15, default=0)

    def __str__(self):
        return self.montant_total

the views :

def addSale(request=None):
    current_sale = Sale()
    list_user = User.objects.all().order_by('-username')
    list_product = Product.objects.all().order_by('-name')

    if request.POST:
        if request.POST.get('user') != "":
            current_sale.user = User.objects.get(id=request.POST.get('user'))
        if request.POST.get('product') != "":
            current_sale.product = Product.objects.get(id=request.POST.get('product'))
        if request.POST.get('quantity') != "":
            current_sale.quantity = request.POST.get('quantity')
        if request.POST.get('sale_price') != "":
            current_sale.sale_price = request.GET.get('price')
        if request.POST.get('montant_total') != "":
            current_sale.montant_total = request.POST.get('montant_total')
        current_sale.save()
        return redirect('products-list')
    
    context = {'sale': current_sale, 'list_user': list_user, 'list_product': list_product}
    return render(request, 'daara/add_sale.html', context)

the add_sale.html template:

<div class="card-body">
                       <form method="POST">
                            {% csrf_token %}

                                 <div class="form-group col-md-6">
                                    <label for="utilisateur">Utilisateur</label>
                                    <select
                                        class="form-control" name="user">
                                        <option value="1" selected="selected">Selectionez un utilisateur</option>
                                        {% for user in list_user %}
                                        {% if user.id == sale.user.id %}
                                        <option value="{{sale.user.id}}"  selected="selected">{{sale.user.username}}</option>
                                        {% else %}
                                        <option value="{{user.id}}">{{user.username}}</option>
                                        {% endif %}
                                        {% endfor %}
                                    </select>
                                </div>
                           
                                <div class="form-group col-md-6">
                                    <label for="produit">Produit</label>
                                    <select
                                        class="form-control" name="product" id="product">
                                        <option value="0" selected="selected">Selectionez un produit</option>
                                        {% for product in list_product %}
                                        {% if product.id == sale.product.id %}
                                        <option value="{{sale.product.id}}"  selected="selected">{{sale.product.name}}</option>
                                        {% else %}
                                        <option value="{{product.id}}">{{product.name}}</option>
                                        {% endif %}
                                        {% endfor %}
                                    </select>
                                </div>
                           
                                <div class="form-group col-md-6">
                                    <label for="quantity">Quantity</label>

                                    <input type="number"
                                        class="form-control" id="qte" name="quantity" value="{{sale.quantity}}">
                                </div>
                           
                                <div class="form-group col-md-6">
                                    <label for="sale_price">Price</label><input type="number" step="0.01"
                                          class="form-control" id="product_price" name="sale_price" value="{{product.price}}">
                                </div>
                           
                                 <div class="form-group col-md-6" id="#montant">
                                    <label for="montant">Montant Total</label> <input  type="number" step="0.01"
                                        class="form-control" id="montant" name="montant_total" value="{{sale.montant_total}}">
                                </div>



                            <div class="text-end">
                                 <a class="btn btn-secondary" href="{% url 'products-list' %}" type="button">Close</a>
                                <button class="btn btn-success" type="submit"  value="save">Save</button>

                           </div>
                       </form>
                       <p>{{product_id}}aaa</p>

                    </div>

the url:

path('ad-sale', views.addSale, name="ad-sale")

I’m stuck and I can’t unblock, please I need your help

New contributor

Power Comparison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT