I am testing out ajax with Django + jQuery. I have this in my views:
def ajax_test(request):
if request.is_ajax():
message = "This is ajax"
else:
message = "Not ajax"
return HttpResponse(message)
and this in my template:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="{{ STATIC_URL }}js/jquery-1.6.4.js"></script>
</head>
<body>
<script>
$.get("/ajax_test", function(data) {
alert(data);
});
});
</script>
</body>
</html>
My question is – why does this return “Not ajax” to my html page?
I don’t know what happened to you. I tested and got a good message:
This is ajax. My code:urls.py
views.py
templates/home.html