Learner Learner - 1 year ago 37
Javascript Question

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


def fetch_price(request, pk):
response = {}
product = get_object_or_404(Product, pk=pk)
if request.method=='GET':
price = product.price
response['price-pk'] = product.pk
response['price'] = price
json_data = json.dumps(response)
return HttpResponse(json_data, content_type='application/json')


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


$('#id_product').on('change', function() {
price_id = $(this).val(); // if shoe is selected price_id value becomes 2 as pk of shoe is 2
url = "/sale/price/"+price_id+"/";
success: function(data){
console.log('price will be updated based on product selected');

Answer Source

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.