如何在不刷新的情况下增加订单详细信息的数量
how do I increase number of order detail without refreshing
我是 Django 的初学者,我创建了结帐页面,当用户更改输入范围编号时
enter image description here 它已发送到服务器,但它适用于第一个服务器
我想是因为我为所有输入设置了相同的 ID,我不能更改它,因为它在标签
中
我该如何解决?
Django 部分
class Ajax_Handler(View):
def get(self,request):
if request.is_ajax():
count=request.GET['count']
return JsonResponse({'count':count},status=200)
ajax部分
$(document).ready(function(){
var crf=$('input[name=csrfmiddlewaretoken]').val();
$("#icount").change(function(){
$.ajax({
url:'/adding',
type:'get',
data:{
count:$('#icount').val(),
},
success:function(respond){
$('#icount').val(respond.count)
}
});
});
});
html部分
<tbody>
{% for order in order.orderdetail_set.all %}
<tr>
<td class="product-image">
{% thumbnail order.product.image "100x100" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
</td>
<td class="product-name"><a href="products/{{order.product.id}}">{{order.product.title}}</a></td>
<td class="product-price">{{order.product.price|intcomma:False}}تومان</td>
<td class="product-quantity">
<label>تعداد</label>
<input type="hidden" name="order_id{{order.id}}" value="{{order.id}}">
<input name='count' id='icount' min="1" max="100" value="{{order.count}}" type="number">
</td>
<td class="product-total">{{order.totalprice|intcomma:False}}</td>
<td class="product-remove"><a href="delete/{{order.id}}"><i class="fa fa-trash-o"></i></a></td>
</tr>
{% endfor %}
</tbody>
你可以这样做
<input name='count' id='icount' min="1" max="100" value="{{order.count}}" type="number" onchange="handleOrderCount({{order.id}})">
function handleOrderCount(OrderId){
$.ajax({
url:'/adding',
type:'PUT',
data:{
'OrderId': OrderId,
'csrfmiddlewaretoken': '{{ csrf_token }}',
'count': $('#icount').val(),
},
success:function(respond){
$('#icount').val(respond.count)
}
error:function(respond){
console.log(respond)
}
});
}
如果您想更改数据库中的任何内容,请不要使用 GET 方法,它用于从数据库或服务器检索数据,使用 PUT
更新任何对象
POST - create
GET - read
PUT - update
DELETE - delete
在你看来你可以这样
class Ajax_Handler(View):
def get(self,request):
if request.is_ajax() and request.method == "POST":
count = request.POST['count']
# can filter object and update it like this
# order_id = request.POST['OrderId']
# Order.object.filter(id=order_id)
return JsonResponse({'count':count},status=200)
我是 Django 的初学者,我创建了结帐页面,当用户更改输入范围编号时 enter image description here 它已发送到服务器,但它适用于第一个服务器 我想是因为我为所有输入设置了相同的 ID,我不能更改它,因为它在标签
中我该如何解决?
Django 部分
class Ajax_Handler(View):
def get(self,request):
if request.is_ajax():
count=request.GET['count']
return JsonResponse({'count':count},status=200)
ajax部分
$(document).ready(function(){
var crf=$('input[name=csrfmiddlewaretoken]').val();
$("#icount").change(function(){
$.ajax({
url:'/adding',
type:'get',
data:{
count:$('#icount').val(),
},
success:function(respond){
$('#icount').val(respond.count)
}
});
});
});
html部分
<tbody>
{% for order in order.orderdetail_set.all %}
<tr>
<td class="product-image">
{% thumbnail order.product.image "100x100" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
</td>
<td class="product-name"><a href="products/{{order.product.id}}">{{order.product.title}}</a></td>
<td class="product-price">{{order.product.price|intcomma:False}}تومان</td>
<td class="product-quantity">
<label>تعداد</label>
<input type="hidden" name="order_id{{order.id}}" value="{{order.id}}">
<input name='count' id='icount' min="1" max="100" value="{{order.count}}" type="number">
</td>
<td class="product-total">{{order.totalprice|intcomma:False}}</td>
<td class="product-remove"><a href="delete/{{order.id}}"><i class="fa fa-trash-o"></i></a></td>
</tr>
{% endfor %}
</tbody>
你可以这样做
<input name='count' id='icount' min="1" max="100" value="{{order.count}}" type="number" onchange="handleOrderCount({{order.id}})">
function handleOrderCount(OrderId){
$.ajax({
url:'/adding',
type:'PUT',
data:{
'OrderId': OrderId,
'csrfmiddlewaretoken': '{{ csrf_token }}',
'count': $('#icount').val(),
},
success:function(respond){
$('#icount').val(respond.count)
}
error:function(respond){
console.log(respond)
}
});
}
如果您想更改数据库中的任何内容,请不要使用 GET 方法,它用于从数据库或服务器检索数据,使用 PUT
更新任何对象
POST - create
GET - read
PUT - update
DELETE - delete
在你看来你可以这样
class Ajax_Handler(View):
def get(self,request):
if request.is_ajax() and request.method == "POST":
count = request.POST['count']
# can filter object and update it like this
# order_id = request.POST['OrderId']
# Order.object.filter(id=order_id)
return JsonResponse({'count':count},status=200)