Reputation: 357
I am building a really simple AJAX call using Python/Django. I've never had issues with it before, but now I keep on getting a Not Found error if I ever put actual data in the AJAX call. Works fine if I place no data. I must be making a very obvious mistake that I just can't see.
In my code below, if I comment out the #test value it will work fine, so I know all the URLs are working correctly. It's just if I add any real data it fails (#product_id_notes is just filler, does not refer to an actual ID).
Can anyone help bail me out here? This has been driving my crazy for the last several hours.
My error:
Not Found: /admin/ajax/order_analysis
My html:
<button id = 'test1234' >Submit</button>
<input id = "submit-order-analysis-url" type = 'hidden' value = ' {% url 'admin:order_analysis' %} '>
<div id = "test" value = "123">This is a test</div>
My code
$('#test1234').click(function() {
var url = $('#submit-order-analysis-url').val();
var data = {
'test':$('#test').val(),
'product_id':$('#product_id_notes').val()
}
$.ajax({url: url, data: data})
.done(function(data) {
//do nothing
})
.fail(function() {
//do nothing
console.log("Something went wrong")
});
})
My Django View:
def order_analysis (request):
from apps.seller.models.product import Product
from django.core import serializers
from django.http import JsonResponse
print(request)
from_day = request.GET.get('from-day')
print("FROM DATY!: ",from_day)
order_array = []
try:
all_order_data = Order.objects.filter(created_at__year='2018',
created_at__month='01')
# all_order_data =
Order.objects.filter(sampledate__gte=datetime.date(from_year,
from_month, from_day),
#
sampledate__lte=datetime.date(to_year,to_month, to_day))
for idv_order in all_order_data:
place_holder_dictionary ={}
# print("123")
place_holder_dictionary['order_id'] = str(idv_order.public_id)
place_holder_dictionary['artisan_price'] =
str(idv_order.products_charge)
place_holder_dictionary['product_id'] = str(idv_order.product.id)
place_holder_dictionary['seller'] =
str(idv_order.product.seller.name)
place_holder_dictionary['metric'] =
str(idv_order.product.metric_dimensions)
place_holder_dictionary['photo'] = str(idv_order.product.photo)
place_holder_dictionary['parent'] =
str(idv_order.product.parent_category)
place_holder_dictionary['materials'] =
str(idv_order.product.materials_name_string)
place_holder_dictionary['name'] = str(idv_order.product.name)
print(idv_order.product.parent_category)
order_array.append(place_holder_dictionary)
print("BEFORE ORDER")
order_array_data = json.dumps(order_array)
print("AFTER ORDER")
response = {'product_count': "Filler",
'order_array': order_array_data,
'page_obj':"Filler"
}
return JsonResponse(response)
except Exception as e: print(e) return HttpResponse(status=400)
Upvotes: 0
Views: 240
Reputation: 4095
The mistake is value
is not valid attribute for div
. But, you can do it with data-*
attribute like:
<div id="test" data-value="123">This is a test</div>
In ajax:
var data = {
'test':$('#test').attr('data-value'),
'product_id':$('#product_id_notes').val()
}
In your post, val()
is not working. But taking from the attribute instead of val()
should work:
var data = {
'test':$('#test').attr('value'),
'product_id':$('#product_id_notes').val()
}
The main problem was:
value = ' {% url 'admin:order_analysis' %} '
Your value should not contain space. So, do:
value = '{% url 'admin:order_analysis' %}'
*Note:- HTML is forgiving language, so later answer will also work. But, it is never the correct way and it violates DTD.
Upvotes: 1