簡體   English   中英

在Django上找不到頁面(404)錯誤

[英]Page not found (404) error on django

一旦嘗試以銷售形式選擇產品,我便會試圖獲取價格值。 有一種包含價格,數量和產品領域的銷售表格。 當用戶選擇產品時,該產品的價格應顯示在價格輸入框中。 為此,我使用了ajax。

但是我收到在sales / price / 2中找不到的404頁面錯誤。 當我在瀏覽器中輸入該網址時,我得到的結果為{“ 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+)/$'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM