Serenity
Serenity

Reputation: 4054

Page not found (404) error on django

I am trying to get price value as soon as product is selected in sale form. There is a sale form which has field of price, quantity and product. When user selects for product, the price of that product should be shown to the input box of price. For that i have used 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}

The code

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>

Upvotes: 0

Views: 872

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599590

Your URL pattern doesn't end with a slash, but your Ajax request is for a URL that does end with a slash. Fix one or the other; probably better for consistency to ensure that the pattern has a slash.

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

Upvotes: 3

Related Questions