{"id":194,"date":"2012-06-12T07:36:56","date_gmt":"2012-06-12T11:36:56","guid":{"rendered":"http:\/\/lichun.cc\/blog\/?p=194"},"modified":"2012-06-12T07:36:56","modified_gmt":"2012-06-12T11:36:56","slug":"a-simple-ajax-example-on-django-1-4","status":"publish","type":"post","link":"https:\/\/www.lichun.cc\/blog\/2012\/06\/a-simple-ajax-example-on-django-1-4\/","title":{"rendered":"A simple Ajax example on Django 1.4"},"content":{"rendered":"<p>I used to use <strong>php<\/strong> to handle ajax request, which is pretty easy. Using Django framework is a little complex, here is a simple example, and I&#8217;m sure there are other ways.<\/p>\n<p>1. In your views.py:<\/p>\n<p>(1) include the following lines<\/p>\n<pre>from json import dumps\nfrom django.views.decorators.csrf import csrf_exempt<\/pre>\n<p><!--more--><\/p>\n<p>(2) write a function to handle the requests:<\/p>\n<pre>@csrf_exempt #this is important\ndef quest(request):\n\tif request.is_ajax() and request.method == \"POST\" and request.POST[\"operation\"] == \"loadmovies\":\n\t\tmimetype = \"application\/json\";\n\t\tdata = dumps(retrieveMovieInfo(Movie.objects.all()));\n\t\treturn HttpResponse(data, mimetype);\n\telse:\n\t\treturn HttpResponse(\"This is not an valid request\");<\/pre>\n<p>2. In your urls.py, add the following lines:<\/p>\n<pre>url(r'^quest\/$', 'quest'),<\/pre>\n<p>3. In your javascript, make a request, I am using jQuery here:<\/p>\n<pre>\n$.post(\"\/movie\/quest\/,\n\tdata : {\n            operation: \"loadmovies\"\n        },\n\tfunction (results) {\n\t\t\/\/the results is already a js array, no need to JSON.parse\n\t});\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I used to use php to handle ajax request, which is pretty easy. Using Django framework is a little complex, here is a simple example, and I&#8217;m sure there are other ways. 1. In your views.py: (1) include the following lines from json import dumps from django.views.decorators.csrf import csrf_exempt<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[26],"tags":[38,84],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2s9sh-38","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/posts\/194"}],"collection":[{"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/comments?post=194"}],"version-history":[{"count":0,"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/posts\/194\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/media?parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/categories?post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lichun.cc\/blog\/wp-json\/wp\/v2\/tags?post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}