I don't know what happened to you. I tested and got a good message: This is ajax
. My code:
urls.py
url(r'^$', 'myapp.views.home', name='home'),
url(r'^ajax_test/$', 'myapp.views.ajax_test', name='ajax_test'),
views.py
def home(request):
return render_to_response('home.html', {},
context_instance=RequestContext(request))
def ajax_test(request):
if request.is_ajax():
message = "This is ajax"
else:
message = "Not ajax"
return HttpResponse(message)
templates/home.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function () {
$.get("/ajax_test/", function (data) {
alert(data);
});
});
</script>
</body>
</html>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…