gpt4 book ai didi

javascript - django 上找不到页面(404)错误

转载 作者:行者123 更新时间:2023-12-02 14:14:12 25 4
gpt4 key购买 nike

一旦以销售形式选择产品,我就试图获取价格值(value)。有一个销售表格,其中包含价格、数量和产品字段。当用户选择产品时,该产品的价格应显示在价格输入框中。为此我使用了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}

代码

sales/views.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')

sales/urls.py

url(r'^price/(?P<pk>\d+)$', views.fetch_price, name='fetch_price'),

add_sale.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。修复其中之一;确保模式有斜线可能会更好地保持一致性。

r'^price/(?P<pk>\d+)/$'

关于javascript - django 上找不到页面(404)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39191613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com