我正在努力得到价格的价值,一旦产品被选择在销售形式。有一种销售形式,它包括价格、数量和产品。当用户选择产品时,该产品的价格应显示在价格输入框中。为此,我使用了ajax。在
But i am getting an error of 404 page not found in sales/price/2. When i enter that url in browser i get the result as {"price-pk": 2, "price": 890.0}
代码
销售/视图.py
def fetch_price(request, pk):
response = {}
product = get_object_or_404(Product, pk=pk)
print('product',product)
if request.method=='GET':
price = product.price
print('price',price)
response['price-pk'] = product.pk
response['price'] = price
json_data = json.dumps(response)
return HttpResponse(json_data, content_type='application/json')
销售/网址.py
^{pr2}$添加_销售.html
<script>
$('#id_product').on('change', function() {
price_id = $(this).val(); // if shoe is selected price_id value becomes 2 as pk of shoe is 2
console.log(price_id);
url = "/sale/price/"+price_id+"/";
$.ajax({
type:'GET',
url:url,
success: function(data){
console.log('price will be updated based on product selected');
$('#id_price').val(data.price);
}
})
});
</script>
您的URL模式不以斜杠结尾,但Ajax请求的URL以斜杠结尾。修复其中一个;为了保持一致性,最好确保模式有一个斜线。在
相关问题 更多 >
编程相关推荐