

{"id":59806,"date":"2019-06-19T10:21:22","date_gmt":"2019-06-19T04:51:22","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=59806"},"modified":"2021-06-21T12:19:26","modified_gmt":"2021-06-21T06:49:26","slug":"ajax-in-django","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/","title":{"rendered":"AJAX in Django &#8211; Learn How it Works using jQuery!"},"content":{"rendered":"<p style=\"text-align: left;\">AJAX has now become an important part of web applications. AJAX also helped Django a lot to increase efficiency and save the time of users. All the popular services out there use it in some way or the other.<\/p>\n<p style=\"text-align: left;\">Applications like Facebook, Twitter, Instagram, Gmail, Google Maps, Spotify, etc cannot function without AJAX.<\/p>\n<p style=\"text-align: left;\">You will have to deal with AJAX code at some point. Even Google search uses AJAX. You must have seen those predictions down the search bar while typing, those are AJAX generated results. We are going to learn the implementation of AJAX in Django in this tutorial.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Google-Search.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59865\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Google-Search.png\" alt=\"AJAX in Google Search\" width=\"1920\" height=\"943\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Google-Search.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Google-Search-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Google-Search-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Google-Search-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Google-Search-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Google-Search-520x255.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p style=\"text-align: left;\">This tutorial will become easy to understand if you know JavaScript. It will be amazing if you also have some experience with<strong> DOM (Document Object Model)<\/strong>.<\/p>\n<h3 style=\"text-align: left;\">AJAX in Django<\/h3>\n<p style=\"text-align: left;\">We talked about AJAX and how it is present in our web pages. Now, let\u2019s learn why we need AJAX in Django.\u00a0AJAX is an acronym of <strong>Asynchronous JavaScript and XML<\/strong>.<\/p>\n<p style=\"text-align: left;\">It is a mixture of technologies implemented to solve a very serious problem.\u00a0It utilizes <em>XMLHttpRequest objects<\/em> to transmit and receive data.<\/p>\n<h3 style=\"text-align: left;\">The Problem that AJAX Solved<\/h3>\n<p style=\"text-align: left;\">Imagine that you are reading your emails. Now, think that for every new mail received, you have to reload the entire page which involves some steps:<\/p>\n<ul>\n<li style=\"text-align: left;\">Request from your browser to reload the page.<\/li>\n<li style=\"text-align: left;\">The server receives and responses accordingly with new mails added.<\/li>\n<li style=\"text-align: left;\">The process continues every time you want to read a new mail.<\/li>\n<\/ul>\n<p>This approach is very time-consuming and unproductive. The problem becomes more serious when urgent replies are demanded by the recipient. Apparently, it was a real situation for websites before 2003.<\/p>\n<p>In late 2003, all the browsers solved the above problem by using XHR. XHR is acronym for <strong>XMLHttpRequest<\/strong> Objects. Browsers use the XHR object to communicate with the server. This transmission happens without reloading the whole page.<\/p>\n<p>XMLHttpRequest API is behind the scenes handling the XHR requests.<\/p>\n<p>Now, almost all browsers have XMLHttpRequest APIs. This made applications like Gmail, Google Maps become a reality.<\/p>\n<h3>How AJAX works in Django<\/h3>\n<p>AJAX is nothing but a combination of JavaScript and XHR object. The concept is simple:<\/p>\n<ul>\n<li><strong> JavaScript Code on the client\u00a0<\/strong>&#8211; Side\/ browser makes a request when an event occurs on the web page. The JavaScript code will generate an XHR object and is sent as a request object to the server. The XHR object contains some JavaScript data\/ object.<\/li>\n<li>The XHR object also contains the URL or name of the call-back function on server.<\/li>\n<li><strong>The request is handled by the server with a callback function<\/strong> &#8211; The appropriate view function or callback function handles the request. It will send a success or failure response. Since the request is asynchronous, the rest of the code executes without interruptions.<\/li>\n<li>At that time, the server processes the request.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX-working.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-60074\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX-working.jpg\" alt=\"Django AJAX working\" width=\"914\" height=\"686\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX-working.jpg 914w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX-working-150x113.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX-working-300x225.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX-working-768x576.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX-working-520x390.jpg 520w\" sizes=\"auto, (max-width: 914px) 100vw, 914px\" \/><\/a><\/p>\n<ul>\n<li><strong>The response is received as success and failure<\/strong>\u00a0&#8211; The success response can contain some server data in multiple formats like:<\/li>\n<\/ul>\n<h4><strong>1. Text Format<\/strong><\/h4>\n<p>Html Format (This contains HTML elements).<\/p>\n<h4><strong>2. JSON Format<\/strong><\/h4>\n<p>JSONP Format (It is JSON but only when the response comes from a different domain).<\/p>\n<p>Script (This contains some JavaScript to be changed in page).<\/p>\n<h4><strong>3. XML Format<\/strong><\/h4>\n<p>Similarly, a failure response can be formatted.<\/p>\n<ul>\n<li><strong>The JavaScript will execute according to the response received &#8211;<\/strong> The AJAX routine will now execute according to data from the server. We can change the HTML elements or run some JavaScript. Many things are possible using it. The best example is Facebook\u2019s like button.<\/li>\n<\/ul>\n<p>AJAX has multiple implementations but this is the common flow.<\/p>\n<h4>Example of AJAX in Real World<\/h4>\n<p>AJAX is very common in web applications. For example &#8211; when you like a post on LinkedIn, there are certain actions performed at the back-end but at the front-end, the color\u00a0of Like button changes.<\/p>\n<p>When you clicked that button, an XHR object was sent to the server. The server receives the request and registers a like for that post. Then, the server sends a success response and the <strong>Like<\/strong> button turns to <strong>Blue <\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linkedin-Like-button.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59866\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linkedin-Like-button.png\" alt=\"Linkedin Like button - Django AJAX\" width=\"819\" height=\"747\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linkedin-Like-button.png 819w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linkedin-Like-button-150x137.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linkedin-Like-button-300x274.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linkedin-Like-button-768x700.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Linkedin-Like-button-520x474.png 520w\" sizes=\"auto, (max-width: 819px) 100vw, 819px\" \/><\/a><\/p>\n<h4>Using jQuery for AJAX in Django<\/h4>\n<p>jQuery is one of the most popular libraries of JavaScript. Many advanced frameworks like <em>Angular JS, React,<\/em> etc are based on jQuery. It is a very useful library providing functions with DOM of web pages. To use bootstrap\u2019s JavaScript part, jQuery is essential.<\/p>\n<p>We are using jQuery for the following reasons:<\/p>\n<ul>\n<li><strong>AJAX implementation is different on multiple browsers<\/strong><\/li>\n<\/ul>\n<p>There are many browsers which use different XHR APIs. These APIs parse request and response in a slightly different manner. Keeping your code up-to-date while also making it platform independent can be difficult. jQuery provides you with a built-in solution and takes care of all these things for you.<\/p>\n<p>Since jQuery is actively developed thus, the code is kept updated frequently.<\/p>\n<ul>\n<li><strong>jQuery provides a vast range of methods using DOM<\/strong><\/li>\n<\/ul>\n<p>jQuery contains many DOM objects and related methods. These objects can be used as per your need and jQuery makes it more convenient to use.<\/p>\n<ul>\n<li><strong>jQuery is very popular<\/strong><\/li>\n<\/ul>\n<p>jQuery is heavily used in the IT industry. For any web developer, they will deal with jQuery at some point. <a href=\"https:\/\/jquery.com\/\">jQuery<\/a> has many features and it will be an important skill for you in the long run.<\/p>\n<ul>\n<li><strong>jQuery is basic for many popular frameworks\u00a0<\/strong><\/li>\n<\/ul>\n<p>There are many popular frameworks like Angular Js, react js based on jQuery. Bootstrap JavaScript is also based on jQuery. jQuery is basically everywhere on JavaScript frameworks.<\/p>\n<p>These reasons are good enough for anyone to learn and use jQuery. There are more ways by which we can use AJAX in Django like, dajax, dajaxice, etc. These applications are made such that there is minimal JavaScript for AJAX.<\/p>\n<p>In the real world, developers prefer jQuery, therefore, we are going to make an application based on that.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-official-website.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59867\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-official-website.png\" alt=\"jQuery official website - Django AJAX\" width=\"1920\" height=\"943\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-official-website.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-official-website-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-official-website-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-official-website-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-official-website-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-official-website-520x255.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<h3>Simple Post App and Like Button<\/h3>\n<p>We are going to create a simple project where users can like a post. For this, we will be adding posts through admin model. Let&#8217;s follow these steps:<\/p>\n<ul>\n<li><strong>Make a new app \u2018post\u2019<\/strong><\/li>\n<\/ul>\n<p>We all know how to<em> <strong>create a new application in Django<\/strong><\/em>. In your PowerShell\/ Terminal, execute this command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py startapp post<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/make-a-new-app-post.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59868\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/make-a-new-app-post.png\" alt=\"make a new app post\" width=\"1880\" height=\"999\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/make-a-new-app-post.png 1880w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/make-a-new-app-post-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/make-a-new-app-post-300x159.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/make-a-new-app-post-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/make-a-new-app-post-1024x544.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/make-a-new-app-post-520x276.png 520w\" sizes=\"auto, (max-width: 1880px) 100vw, 1880px\" \/><\/a><\/p>\n<p>Now, install the application in settings.py of project. Add the name of the application in INSTALLED_APPS list.<\/p>\n<p style=\"text-align: left;\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-in-Installed-apps.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59869\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-in-Installed-apps.png\" alt=\"post in Installed-apps - Django AJAX\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-in-Installed-apps.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-in-Installed-apps-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-in-Installed-apps-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-in-Installed-apps-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-in-Installed-apps-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-in-Installed-apps-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<ul>\n<li><strong>Creating models \u2013 \u2018Post\u2019 and \u2018Like\u2019<\/strong><\/li>\n<\/ul>\n<p>Models are the tables in our databases.\u00a0Paste this code in post\/ models.py file.<\/p>\n<p><strong>Code<\/strong><em><strong>:<\/strong> <\/em><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.db import models\r\n\r\n# Create your models here. #DataFlair class Post(models.Model): post_heading = models.CharField(max_length = 200)\r\n        post_text = models.TextField()\r\n        post_author = models.CharField(max_length = 100, default = 'anonymous')\r\n\r\n\r\nclass Like(models.Model):\r\npost = models.ForeignKey(Post, on_delete = 'CASCADE')\r\n\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/models-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59871\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/models-py.png\" alt=\"models.py - Django AJAX\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/models-py.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/models-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/models-py-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/models-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/models-py-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/models-py-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Here, Post and Like are two models. We are using the <strong>Foreign Key Field<\/strong>. It is used to define many-to-one relationships between models. The post variable in Like model will contain the primary key of the post that\u00a0we added. Thus, we can then refer to that post&#8217;s data.<\/p>\n<p>We are not using it in this example though.<\/p>\n<ul>\n<li><strong>Creating view functions \u2013 \u2018index\u2019 and \u2018like\u2019<\/strong><\/li>\n<\/ul>\n<p>Open post\/ view.py file and paste this code in it.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.shortcuts import render\r\nfrom .models import Post, Like\r\nfrom django.http import HttpResponse\r\n# Create your views here.\r\n\r\n#DataFlair #AJAX_tutorial\r\ndef index(request):\r\n    posts = Post.objects.all()\r\n    return render(request, 'post\/index.html', { 'posts': posts })\r\n\r\ndef like(request):\r\n    if request.method == 'GET':\r\n        post_id = request.GET['post_id']\r\n        likedpost = Post.objects.get(id = post_id )\r\n        m = Like( post=likedpost )\r\n        m.save()\r\n        return HttpResponse('success')\r\n    else:\r\n        return HttpResponse(\"unsuccesful\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/views-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59872\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/views-py.png\" alt=\"views.py - Django AJAX\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/views-py.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/views-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/views-py-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/views-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/views-py-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/views-py-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>We created two view functions. The first one is to render all the posts and the second one is for the\u00a0like function.<\/p>\n<p>The like function exists to handle the AJAX request from like button. As you can see, it is similar to the function that we use in forms.<\/p>\n<p>The like function will first check for the request. If it is the GET method, then it will read the data. The data is the id of the post we liked. Then, we create a Like class object and save its id equal to data. Then, we store the object in our database.<\/p>\n<p>At last, we are returning an HttpResponse. This response will do nothing in our code as we are not printing the same. We could have just passed a script. These can be utilized according to one\u2019s use case.<\/p>\n<ul>\n<li><strong>Creating templates \u2013 \u2018index.html\u2019<\/strong><\/li>\n<\/ul>\n<p>Here comes the main part, templates, as AJAX is a front-end technology. To implement this, create some directories.<\/p>\n<ol>\n<li>Inside post directory, create templates directory.<\/li>\n<li>Inside the templates directory, create post directory.<\/li>\n<li>Create index.html inside post\/templates\/post directory\/folder.<\/li>\n<\/ol>\n<p>Now, paste the html in index.html<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;Like Post App&lt;\/title&gt;\r\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/css\/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH\/1fQ784\/j6cY\/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\"&gt;\r\n&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.4.1.js\" integrity=\"sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;div class = \"display-3 color-red\"&gt;&lt;center&gt;DataFlair AJAX Tutorial&lt;br&gt;Post APP&lt;\/center&gt;&lt;\/div&gt;\r\n    {% for post in posts %}\r\n        &lt;div class = 'container jumbotron'&gt;\r\n        &lt;h3 class=\"display-5\"&gt;{{ forloop.counter }}. {{ post.post_heading }}&lt;\/h3&gt;\r\n&lt;p class=\"lead\"&gt;{{ post.post_text }} &lt;\/p&gt;\r\n        &lt;p&gt;\r\n        &lt;div type=\"text\/css\" class = \"container\"&gt;Author : {{ post.post_author }}&lt;\/div&gt; \r\n        &lt;a class=\"likebutton btn btn-primary btn-lg\" id=\"like{{ post.id }}\" href=\"#\" data-catid=\"{{ post.id }}\"&gt;Like&lt;\/a&gt; &lt;\/p&gt; &lt;p id=\"message{{post.id}}\"&gt;\r\n&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n{% endfor %}\r\n\r\n&lt;script type=\"text\/javascript\"&gt; \r\n$('.likebutton').click(function(){ \r\nvar id; \r\nid = $(this).attr(\"data-catid\"); \r\n$.ajax( \r\n{ \r\n    type:\"GET\", \r\n    url: \"like\", \r\n    data:{ \r\n             post_id: id \r\n}, \r\nsuccess: function( data ) \r\n{ \r\n    $( '#like'+ id ).removeClass('btn btn-primary btn-lg'); \r\n    $( '#like'+ id ).addClass('btn btn-success btn-lg'); } }) });\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/index-html.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59873\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/index-html.png\" alt=\"index.html\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/index-html.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/index-html-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/index-html-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/index-html-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/index-html-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/index-html-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>There are two important parts in this HTML file.<\/p>\n<ul>\n<li><strong> Importing jQuery and Bootstrap<\/strong><\/li>\n<\/ul>\n<p>Since jQuery is a JavaScript library, so it is essentially a static file for the server. It can be imported the same as bootstrap and other files. We chose to use it directly from CDN. The link can be found on jQuery&#8217;s official website.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-CDN-.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59874\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-CDN-.png\" alt=\"jQuery CDN\" width=\"1920\" height=\"943\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-CDN-.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-CDN--150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-CDN--300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-CDN--768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-CDN--1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/jQuery-CDN--520x255.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Similarly, we used the link tag with bootstrap.<\/p>\n<ul>\n<li><strong>Generating AJAX request from JavaScript<\/strong><\/li>\n<\/ul>\n<p>Here, is the important part. If you have no idea of DOM, <em><strong>JavaScript Objects<\/strong><\/em>, then this might be difficult to understand. We will try our best to make it relatable.<\/p>\n<p>Focus on &lt;script&gt;&lt;\/script&gt; at the end of document. We have put our AJAX code there.<\/p>\n<p style=\"text-align: left;\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/AJAX-part.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59875\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/AJAX-part.png\" alt=\"AJAX part in the template\" width=\"1074\" height=\"473\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/AJAX-part.png 1074w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/AJAX-part-150x66.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/AJAX-part-300x132.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/AJAX-part-768x338.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/AJAX-part-1024x451.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/AJAX-part-520x229.png 520w\" sizes=\"auto, (max-width: 1074px) 100vw, 1074px\" \/><\/a><\/p>\n<p>We are using some of the properties of jQuery to call the object. The like button is the class of button that we are going to click. When the button is clicked, the function inside it is executed. That function has some objects in it.<\/p>\n<p>This is an <strong>XHR<\/strong> object. Actually, this is the speciality of <strong>$.ajax() method<\/strong>. It is what will make our code run on multiple browsers. Inside that, there is a JavaScript Object Datatype. JavaScript Object is similar to dictionaries in Python. This is for Pythonistas who don\u2019t know JavaScript.<\/p>\n<p>Now, you can relate it easily.<\/p>\n<p><strong>1. Type key:<\/strong>\u00a0It takes in the method of request that we will send. Here, XHR is sent over the GET request.<\/p>\n<p><strong>2. URL key:<\/strong> This is the URL of the call back function on the server. Remember the view function we created? This code will call that function without reloading the page.<\/p>\n<p><strong>3. Data key:<\/strong> This key contains the data that we are sending to call back function. In this example, it contains the id of the post. It can be an object and can contain multiple values. You can also pass it in a JSON. It depends on your use case.<\/p>\n<p><strong>4. Success method:<\/strong> JavaScript Objects have methods too. You can define methods like this easily. The methods are called properties of Objects. Success is a built-in response for<strong> $.ajax() method<\/strong>. This method will be executed when the AJAX request returns with a success response. It is one of the conveniences that jQuery provides to programs.<\/p>\n<p><strong>5. Inside Success:<\/strong> There are two methods that we are executing inside success. The first one will remove the class inside the parenthesis (). Similarly, the other one will add the class.<\/p>\n<p>It is as simple as that. No, worries if you didn\u2019t get it right the first time.<br \/>\n<a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/ajax-help-page.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59876\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/ajax-help-page.png\" alt=\"ajax documentation help page\" width=\"1920\" height=\"943\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/ajax-help-page.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/ajax-help-page-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/ajax-help-page-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/ajax-help-page-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/ajax-help-page-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/ajax-help-page-520x255.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<ul>\n<li><strong>C<\/strong><strong>onnecting it with urls-config<\/strong><\/li>\n<\/ul>\n<p>Now, comes the Python part. You just created a very beautiful app. Now we have to link all the components together.<\/p>\n<p>Make a new python file in post directory.<\/p>\n<p><strong>Inside post\/urls.py, paste this Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.conf.urls import url\r\nfrom django.urls import path\r\nfrom . import views\r\nurlpatterns = [\r\n#DataFlair #AJAX tutorial\r\n        path('', views.index, name='index'),\r\n        path('like\/', views.like, name='like'), ]\r\n\r\n<\/pre>\n<p style=\"text-align: left;\"><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-urls-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59877\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-urls-py.png\" alt=\"post.urls.py - Django AJAX\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-urls-py.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-urls-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-urls-py-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-urls-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-urls-py-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/post-urls-py-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Now, configure your main urls.py file.<\/p>\n<p><strong>Add this to your urlpatterns list:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">path('ajax\/', include('post.urls')),<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/urls-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59878\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/urls-py.png\" alt=\"urls.py - Django AJAX\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/urls-py.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/urls-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/urls-py-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/urls-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/urls-py-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/urls-py-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<ul>\n<li><strong>Populating the database via admin<\/strong><\/li>\n<\/ul>\n<p>Now, open post\/admin.py file.<\/p>\n<p><strong>Paste this code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.contrib import admin\r\nfrom .models import *\r\n# Register your models here.\r\n#DataFlair #AJAX\r\nadmin.site.register(Post)\r\nadmin.site.register(Like)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/admin-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59879\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/admin-py.png\" alt=\"admin.py - Django AJAX\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/admin-py.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/admin-py-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/admin-py-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/admin-py-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/admin-py-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/admin-py-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>We are just registering our models in Django admin.<\/p>\n<p>Now, open console\/ PowerShell\/ Terminal and run these commands in order.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py makemigrations\r\npython manage.py migrate\r\n\r\n<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/migrating.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59880\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/migrating.png\" alt=\"migrating code terminal\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/migrating.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/migrating-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/migrating-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/migrating-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/migrating-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/migrating-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Start your server and add some posts from admin.<\/p>\n<p><strong>URL:<\/strong> http:\/\/localhost:8000\/admin\/<\/p>\n<p style=\"text-align: left;\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/adding-posts.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59881\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/adding-posts.png\" alt=\"adding posts via admin\" width=\"1920\" height=\"1080\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/adding-posts.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/adding-posts-150x84.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/adding-posts-300x169.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/adding-posts-768x432.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/adding-posts-1024x576.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/adding-posts-520x293.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><br \/>\n<\/a><\/p>\n<p>Populate your database with some posts.<\/p>\n<ul>\n<li><strong>Running server<\/strong><\/li>\n<\/ul>\n<p>We already started the server. Now, search for <strong>URL<\/strong>:<\/p>\n<p>http:\/\/localhost:8000\/ajax\/<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59882\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-1.png\" alt=\"Running server output 1\" width=\"1619\" height=\"882\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-1.png 1619w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-1-150x82.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-1-300x163.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-1-768x418.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-1-1024x558.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-1-520x283.png 520w\" sizes=\"auto, (max-width: 1619px) 100vw, 1619px\" \/><\/a><\/p>\n<p>Isn\u2019t it great? We just created a very beautiful web page with AJAX functionality. Try clicking on one of the like buttons.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-59884\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-2.png\" alt=\"Running server output 2\" width=\"1619\" height=\"882\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-2.png 1619w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-2-150x82.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-2-300x163.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-2-768x418.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-2-1024x558.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/output-2-520x283.png 520w\" sizes=\"auto, (max-width: 1619px) 100vw, 1619px\" \/><\/a><\/p>\n<p><em> Awesome, it turned to green. Now, you can actually build something amazing with Django which also looks good. Our Like button works. !! Awesome !! <\/em><\/p>\n<h3>Summary<\/h3>\n<p>I appreciate that you completed the tutorial. There were some parts which I think can be difficult for people who don\u2019t know JavaScript. In this tutorial, we took a brief look at AJAX in Django. We have observed AJAX real life problems, solutions and also how it works in Jquery.<\/p>\n<p>Do let us know your opinion on this tutorial through comments.<span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1500,&quot;href&quot;:&quot;https:\\\/\\\/jquery.com&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251205175441\\\/https:\\\/\\\/jquery.com\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 09:31:36&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-27 19:30:33&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-02 12:00:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-08 11:38:16&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-16 11:32:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-23 10:26:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-02 09:51:46&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-25 08:05:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-28 04:38:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-03 14:50:46&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-16 02:06:25&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-19 02:13:19&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-22 15:12:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-26 05:55:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-10 09:34:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-22 11:53:24&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-22 11:53:24&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>AJAX has now become an important part of web applications. AJAX also helped Django a lot to increase efficiency and save the time of users. All the popular services out there use it in&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":59885,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19149],"tags":[20203,20204,20205,20206],"class_list":["post-59806","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django","tag-ajax-in-django","tag-ajax-real-world-example","tag-ajax-working-in-django","tag-using-jquery-for-ajax-in-django"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>AJAX in Django - Learn How it Works using jQuery! - DataFlair<\/title>\n<meta name=\"description\" content=\"In Django AJAX tutorial, learn to work on AJAX in Django, real world example of AJAX, using jQuery for AJAX in Django &amp; create project for Post app &amp; Like button.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/ajax-in-django\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AJAX in Django - Learn How it Works using jQuery! - DataFlair\" \/>\n<meta property=\"og:description\" content=\"In Django AJAX tutorial, learn to work on AJAX in Django, real world example of AJAX, using jQuery for AJAX in Django &amp; create project for Post app &amp; Like button.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/ajax-in-django\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-19T04:51:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-21T06:49:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"AJAX in Django - Learn How it Works using jQuery! - DataFlair","description":"In Django AJAX tutorial, learn to work on AJAX in Django, real world example of AJAX, using jQuery for AJAX in Django & create project for Post app & Like button.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/","og_locale":"en_US","og_type":"article","og_title":"AJAX in Django - Learn How it Works using jQuery! - DataFlair","og_description":"In Django AJAX tutorial, learn to work on AJAX in Django, real world example of AJAX, using jQuery for AJAX in Django & create project for Post app & Like button.","og_url":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-06-19T04:51:22+00:00","article_modified_time":"2021-06-21T06:49:26+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"AJAX in Django &#8211; Learn How it Works using jQuery!","datePublished":"2019-06-19T04:51:22+00:00","dateModified":"2021-06-21T06:49:26+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/"},"wordCount":2003,"commentCount":13,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX.jpg","keywords":["AJAX in Django","AJAX Real world Example","AJAX working in Django","Using jQuery for AJAX in Django"],"articleSection":["Django Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/ajax-in-django\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/","url":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/","name":"AJAX in Django - Learn How it Works using jQuery! - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX.jpg","datePublished":"2019-06-19T04:51:22+00:00","dateModified":"2021-06-21T06:49:26+00:00","description":"In Django AJAX tutorial, learn to work on AJAX in Django, real world example of AJAX, using jQuery for AJAX in Django & create project for Post app & Like button.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/ajax-in-django\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/06\/Django-AJAX.jpg","width":802,"height":420,"caption":"AJAX in Django"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/ajax-in-django\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Django Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/django\/"},{"@type":"ListItem","position":3,"name":"AJAX in Django &#8211; Learn How it Works using jQuery!"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/59806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=59806"}],"version-history":[{"count":22,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/59806\/revisions"}],"predecessor-version":[{"id":97688,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/59806\/revisions\/97688"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/59885"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=59806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=59806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=59806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}