Django

AJAX in Django

AJAX in Django

AJAX is an abbreviation used for Asynchronous JavaScript And XML that is used for web pages to update asynchronously by exchanging the data with the server. In general, AJAX is used to ease the end-user experience. 
Django supports the AJAX framework used to develop asynchronous presentation logic for web applications more quickly. And it doesn’t require python and JavaScript source code for its implementation. Django-dajax is a very powerful tool for creating logic in web applications. 
Let’s see how we can use Django-dajax. First, we are going to install Django-dajax using pip:

$ pip install django_dajax
$ easy_install django_dajax

After installing Django-dajax, we will configure both dajax and dajaxice using the configurations in the settings.py file:

INSTALLED_APPS += ( ‘dajaxice’, ‘dajax’ ) 

We also need to add the following code in the settings.py file:

TEMPLATE_LOADERS = (
   'django.template.loaders.filesystem.Loader',
   'django.template.loaders.eggs.Loader',
   'django.template.loaders.app_directories.Loader',
)
TEMPLATE_CONTEXT_PROCESSORS = (
   'django.contrib.auth.context_processors.auth',
   'django.core.context_processors.debug',
   'django.core.context_processors.i18n',
   'django.core.context_processors.request',
   'django.core.context_processors.media',
   'django.core.context_processors.static',
   'django.contrib.messages.context_processors.messages'
)
STATICFILES_FINDERS = (
   'dajaxice.finders.DajaxiceFinder',
   'django.contrib.staticfiles.finders.FileSystemFinder',
   'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
DAJAXICE_MEDIA_PREFIX = 'dajaxice'

Next, we have to set dajax URLs in the urls.py file:

from dajaxice.core import dajaxice_autodiscover, dajaxice_config
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings

Then dajax urls:

url_patterns += patterns('',
   url(r'^%s/' % settings.DAJAXICE_MEDIA_PREFIX, include('dajaxice.urls')),)   
url_patterns += staticfiles_url_patterns()
Now we are going to create a simple form:
class DreamrealForm(forms.Form):
   site = forms.CharField(max_length = 100)
   u_name = forms.CharField(max_length = 40)
   number = forms.CharField(max_length = 30)
   mail = forms.CharField(max_length = 50)

Next, we need a dajax.py file for our application with the following configurations:

from dajaxice.utils import deserialize_form
from firstapp.form import Imageform
from dajax.core import Dajax
from firstapp.models import FirstModel
@dajaxice_register
def send_myform(request, form):
   dajax = Dajax()
   my_form = ImageForm(deserialize_form(form))
   
   if my_form.is_valid():
      dajax.remove_css_class('#my_form input', 'error')
      fm = FirstModel()
      fm.website = form.cleaned_data.get('site')
      fn.name = form.cleaned_data.get('u_name')
      fn.phonenumber = form.cleaned_data.get('number')
      fn.save()      
      dajax.alert("MyModel Entry %s was successfully saved." % 
         my_form.cleaned_data.get('u_name'))
   else:
      dajax.remove_css_class('#my_form input', 'error')
      for error in form.errors:
         dajax.add_css_class('#id_%s' % error, 'error')            
   return dajax.json()

This way ajax configurations can be set in Django. In case you need asynchronous tasks on a large scale, you can also do socket programming in Django or else other front-end technologies like JavaScript libraries can be used. 

Top course recommendations for you

    Regex in Python
    1 hrs
    Beginner
    3K+ Learners
    4.53  (354)
    Heap Sort Program in C
    1 hrs
    Beginner
    1.3K+ Learners
    4.48  (42)
    Merge Sort Algorithm Using Java
    1 hrs
    Beginner
    1.2K+ Learners
    4.68  (28)
    Python IDE
    3 hrs
    Beginner
    3.5K+ Learners
    Searching Algorithms in Java
    2 hrs
    Beginner
    1.7K+ Learners
    4.66  (68)
    Graphs in Java
    2 hrs
    Intermediate
    2.4K+ Learners
    4.57  (54)
    Java Data Structures for Beginners
    3 hrs
    Beginner
    11K+ Learners
    4.59  (427)
    Java Data Structures for Intermediate Level
    3 hrs
    Intermediate
    6.6K+ Learners
    4.39  (186)