Django adding real time probelm
this is html include that add the product and make multi adding random , once it dosnt make duplicate , and once work normally .
{% load i18n %}
{% load static %}
<form >
<div class="tab_content active" data-tab="fruits">
<div class="row ">
{% for productss in my_products %}
<div class="col-lg-3 col-sm-6 d-flex " >
<div class="productset flex-fill" >
<div class="productsetimg">
{% if productss.product_icon %}
<img src="{% url 'productss.product_icon.url' %}" alt="img">
{% else %}
<img src="{% static 'assets/img/product/product1.jpg' %}" alt="img">
{% endif %}
<h6>{% translate "Qty" %}: {{productss.bpq_real_quantity}}</h6>
<div class="">
<i class=""></i>
</div>
</div>
<div class="productsetcontent">
<h4><span id="productee_namee{{productss.id}}">{{productss}}</span></h4>
<h6>{{productss.bpq_product_sell_price}}</h6>
<div class="increment-decrement">
<div class="input-groups">
<input type="button" value="-" onclick="decrementValue{{productss.id}}()" class="button-minus dec button">
<input type="text" name="child" value="1" class="quantity-field" id="plus_field{{productss.id}}" name="quantity_required">
<input type="button" value="+" onclick="incrementValue{{productss.id}}()" class="button-plus inc button" >
<input type="submit" value="{% translate "Add" %}" id="add_item_{{productss.id}}">
</div>
</div>
</div>
</div>
</div>
</form>
<script>
console.log('plus_field{{productss.id}}')
function incrementValue{{productss.id}}()
{
var value = parseInt(document.getElementById('plus_field{{productss.id}}').value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('plus_field{{productss.id}}').value = value;
}
function decrementValue{{productss.id}}()
{
var value = parseInt(document.getElementById('plus_field{{productss.id}}').value, 10);
value = isNaN(value) ? 0 : value;
if (value > 1 ) {
value--;
}
document.getElementById('plus_field{{productss.id}}').value = value;
}
</script>
{% comment %} var quantity = $("#quantity_required").val();
$.ajax({
type: "POST",
url: {% url 'view_detail' %},
data: search_word,
success: function(result){
alert("Success");
}
}); {% endcomment %}
{% comment %}
$('#add_item_{{productss.id}}').hide();
setTimeout(function()
{
$('#add_item_{{productss.id}}').show();
}, 1000); {% endcomment %}
{% csrf_token %}
<script>
$(document).ready(function(e){
$(document).on('click','#add_item_{{productss.id}}',function(e){
e.preventDefault();
$('#add_item_{{productss.id}}').attr('disabled', true);
setTimeout(function()
{
$('#add_item_{{productss.id}}').attr('disabled', false);
}, 1000);
var $this = $(this);
var quantity = $("#plus_field{{productss.id}}").val();
var customer = $("#cstname").val();
//console.log(productname);
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
$.ajax({
url:"{% url 'orders:add_item_resturant' productss.pk %}",
type: "POST",
dataType: "json",
data: {
'quantity':quantity,
'customer':customer,
'csrfmiddlewaretoken':csrftoken,
},
success:function(response){
//console.log('success addess')
$('#add_item_update').html(response['result']);
totalprice();
},
});
});
});
</script>
{% endfor %}
</div>
</div>
{% comment %}
<div>
<input id="search" name="search" type="text" class="query-search" placeholder="Search...">
<input onclick="setGetParameter()" type="button" class="btn btn-primary" value = "View Details"/>
</div>
<script>
function setGetParameter(){
var search_word = $("#search").val();
$.ajax({
type: "POST",
url: {% url 'view_detail' %},
data: search_word,
success: function(result){
alert("Success");
}
});
}
</script>
and in views :
def search_view(request):
if request.method == "POST":
search_word = request.POST['data']
{% endcomment %}
this is views.py
def add_item_resturant(request,pk):
print(f"This is request.POST: {request.POST}")
current_user=request.user
customer=Customer.objects.get(customer_name=request.POST['customer'])
CstPoints=customer.customer_Points
user_branch=request.user.user_profile.user_branch
current_order=OrderDetail.objects.select_related('order_order').select_related('order_product_name').filter(order_order__order_progress="In Progress",order_order__order_who_created=current_user)
current_product=BrancheProductQuantity.objects.get(id=pk)
branch_tax=current_product.bpq_branch_name.branch_Tax
real_quantity=current_product.bpq_real_quantity
sell_price=current_product.bpq_product_sell_price
returned_order=Order.objects.get(order_who_created=current_user,order_progress="In Progress")
get_quantity = request.POST['quantity']
if float(real_quantity) - float(get_quantity) < 0 :
messages.warning(request,"Product Quantity is Error and the Quantity of it=" + str(real_quantity) +"and you enter" + get_quantity)
return redirect("orders:add_resturant_order")
if request.method =="POST" :
order_detail , created =OrderDetail.objects.get_or_create(
order_order = returned_order,
order_product_name = current_product)
qfloat=float(get_quantity)
sfloat=float(sell_price)
total_item=float(qfloat*sfloat)
tax=(total_item*branch_tax/100)
price_iclude_tax=tax+total_item
order_detail.order_tax_value=float(tax)
order_detail.order_product_name=current_product
order_detail.order_product_price=current_product.bpq_product_sell_price
order_detail.order_product_quantity=float(get_quantity)
order_detail.order_detail_total=float(total_item)
order_detail.total= float(price_iclude_tax)
order_detail.order_biller_user = str(current_user)
order_detail.order_branch = str(user_branch)
order_detail.save()
final_total_value=current_order.aggregate(Sum('total'))['total__sum']
final_tax_value=current_order.aggregate(Sum('order_tax_value'))['order_tax_value__sum']
full_sub_total=current_order.aggregate(Sum('order_detail_total'))['order_detail_total__sum']
returned_order=returned_order.refresh_from_db()
html = render_to_string('include/add_item_resturant.html',{'CstPoints':CstPoints,'final_total_value':final_total_value,'final_tax_value':final_tax_value,'full_sub_total':full_sub_total,'returned_order':returned_order,'order_detailed':current_order, request:request})
return JsonResponse({'result':html})
I want to solve this problem it make adding product twice and may be 3 or 4 times as showd in page I donst know why is this problem appear.