How do I integrate Ajax with Django applications? How do I integrate Ajax with Django applications? python python

How do I integrate Ajax with Django applications?


Even though this isn't entirely in the SO spirit, I love this question, because I had the same trouble when I started, so I'll give you a quick guide. Obviously you don't understand the principles behind them (don't take it as an offense, but if you did you wouldn't be asking).

Django is server-side. It means, say a client goes to a URL, you have a function inside views that renders what he sees and returns a response in HTML. Let's break it up into examples:

views.py:

def hello(request):    return HttpResponse('Hello World!')def home(request):    return render_to_response('index.html', {'variable': 'world'})

index.html:

<h1>Hello {{ variable }}, welcome to my awesome site</h1>

urls.py:

url(r'^hello/', 'myapp.views.hello'),url(r'^home/', 'myapp.views.home'),

That's an example of the simplest of usages. Going to 127.0.0.1:8000/hello means a request to the hello() function, going to 127.0.0.1:8000/home will return the index.html and replace all the variables as asked (you probably know all this by now).

Now let's talk about AJAX. AJAX calls are client-side code that does asynchronous requests. That sounds complicated, but it simply means it does a request for you in the background and then handles the response. So when you do an AJAX call for some URL, you get the same data you would get as a user going to that place.

For example, an AJAX call to 127.0.0.1:8000/hello will return the same thing it would as if you visited it. Only this time, you have it inside a JavaScript function and you can deal with it however you'd like. Let's look at a simple use case:

$.ajax({    url: '127.0.0.1:8000/hello',    type: 'get', // This is the default though, you don't actually need to always mention it    success: function(data) {        alert(data);    },    failure: function(data) {         alert('Got an error dude');    }}); 

The general process is this:

  1. The call goes to the URL 127.0.0.1:8000/hello as if you opened a new tab and did it yourself.
  2. If it succeeds (status code 200), do the function for success, which will alert the data received.
  3. If fails, do a different function.

Now what would happen here? You would get an alert with 'hello world' in it. What happens if you do an AJAX call to home? Same thing, you'll get an alert stating <h1>Hello world, welcome to my awesome site</h1>.

In other words - there's nothing new about AJAX calls. They are just a way for you to let the user get data and information without leaving the page, and it makes for a smooth and very neat design of your website. A few guidelines you should take note of:

  1. Learn jQuery. I cannot stress this enough. You're gonna have to understand it a little to know how to handle the data you receive. You'll also need to understand some basic JavaScript syntax (not far from python, you'll get used to it). I strongly recommend Envato's video tutorials for jQuery, they are great and will put you on the right path.
  2. When to use JSON?. You're going to see a lot of examples where the data sent by the Django views is in JSON. I didn't go into detail on that, because it isn't important how to do it (there are plenty of explanations abound) and a lot more important when. And the answer to that is - JSON data is serialized data. That is, data you can manipulate. Like I mentioned, an AJAX call will fetch the response as if the user did it himself. Now say you don't want to mess with all the html, and instead want to send data (a list of objects perhaps). JSON is good for this, because it sends it as an object (JSON data looks like a python dictionary), and then you can iterate over it or do something else that removes the need to sift through useless html.
  3. Add it last. When you build a web app and want to implement AJAX - do yourself a favor. First, build the entire app completely devoid of any AJAX. See that everything is working. Then, and only then, start writing the AJAX calls. That's a good process that helps you learn a lot as well.
  4. Use chrome's developer tools. Since AJAX calls are done in the background it's sometimes very hard to debug them. You should use the chrome developer tools (or similar tools such as firebug) and console.log things to debug. I won't explain in detail, just google around and find out about it. It would be very helpful to you.
  5. CSRF awareness. Finally, remember that post requests in Django require the csrf_token. With AJAX calls, a lot of times you'd like to send data without refreshing the page. You'll probably face some trouble before you'd finally remember that - wait, you forgot to send the csrf_token. This is a known beginner roadblock in AJAX-Django integration, but after you learn how to make it play nice, it's easy as pie.

That's everything that comes to my head. It's a vast subject, but yeah, there's probably not enough examples out there. Just work your way there, slowly, you'll get it eventually.


Further from yuvi's excellent answer, I would like to add a small specific example on how to deal with this within Django (beyond any js that will be used). The example uses AjaxableResponseMixin and assumes an Author model.

import jsonfrom django.http import HttpResponsefrom django.views.generic.edit import CreateViewfrom myapp.models import Authorclass AjaxableResponseMixin(object):    """    Mixin to add AJAX support to a form.    Must be used with an object-based FormView (e.g. CreateView)    """    def render_to_json_response(self, context, **response_kwargs):        data = json.dumps(context)        response_kwargs['content_type'] = 'application/json'        return HttpResponse(data, **response_kwargs)    def form_invalid(self, form):        response = super(AjaxableResponseMixin, self).form_invalid(form)        if self.request.is_ajax():            return self.render_to_json_response(form.errors, status=400)        else:            return response    def form_valid(self, form):        # We make sure to call the parent's form_valid() method because        # it might do some processing (in the case of CreateView, it will        # call form.save() for example).        response = super(AjaxableResponseMixin, self).form_valid(form)        if self.request.is_ajax():            data = {                'pk': self.object.pk,            }            return self.render_to_json_response(data)        else:            return responseclass AuthorCreate(AjaxableResponseMixin, CreateView):    model = Author    fields = ['name']

Source: Django documentation, Form handling with class-based views

The link to version 1.6 of Django is no longer available updated to version 1.11


I am writing this because the accepted answer is pretty old, it needs a refresher.

So this is how I would integrate Ajax with Django in 2019 :) And lets take a real example of when we would need Ajax :-

Lets say I have a model with registered usernames and with the help of Ajax I wanna know if a given username exists.

html:

<p id="response_msg"></p> <form id="username_exists_form" method='GET'>      Name: <input type="username" name="username" />      <button type='submit'> Check </button>           </form>   

ajax:

$('#username_exists_form').on('submit',function(e){    e.preventDefault();    var username = $(this).find('input').val();    $.get('/exists/',          {'username': username},             function(response){ $('#response_msg').text(response.msg); }    );}); 

urls.py:

from django.contrib import adminfrom django.urls import pathfrom . import viewsurlpatterns = [    path('admin/', admin.site.urls),    path('exists/', views.username_exists, name='exists'),]

views.py:

def username_exists(request):    data = {'msg':''}       if request.method == 'GET':        username = request.GET.get('username').lower()        exists = Usernames.objects.filter(name=username).exists()        if exists:            data['msg'] = username + ' already exists.'        else:            data['msg'] = username + ' does not exists.'    return JsonResponse(data)

Also render_to_response which is deprecated and has been replaced by render and from Django 1.7 onwards instead of HttpResponse we use JsonResponse for ajax response. Because it comes with a JSON encoder, so you don’t need to serialize the data before returning the response object but HttpResponse is not deprecated.