在djang上找不到页面(404)错误

2024-04-26 03:31:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力得到价格的价值,一旦产品被选择在销售形式。有一种销售形式,它包括价格、数量和产品。当用户选择产品时,该产品的价格应显示在价格输入框中。为此,我使用了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>

Tags: ofinidjsonurldataget产品
1条回答
网友
1楼 · 发布于 2024-04-26 03:31:56

您的URL模式不以斜杠结尾,但Ajax请求的URL以斜杠结尾。修复其中一个;为了保持一致性,最好确保模式有一个斜线。在

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

相关问题 更多 >