Django Ajax tutorial, djangoajax
Introduction:
AJAX = Asynchronous JavaScript and XML (Asynchronous JavaScript and XML ).
AJAX is not a new programming language, but a new method that uses existing standards.
AJAX is the art of exchanging data with the server and updating some webpages without reloading the entire page.
Ajax
Most of the time, we do not need to refresh the page when requesting operations on the webpage. Ajax is required to implement this function!
Ajax in jQuery can be used to request or submit data to the background without refreshing a new page. Now it is used for ajax in django. Therefore, download jquey first, and the higher the version, the better.
I. Simple data types sent by ajax:
Html code: Here we only send a simple string
Views. py
# Coding: utf8 from django. shortcuts import render, HttpResponse, render_to_response def Ajax (request): if request. method = 'post': print request. POST return HttpResponse ('execution successfully') else: return render_to_response ('app03/ajax.html ')
Ajax.html
<! DOCTYPE html>
Run, result:
Ii. complex data types sent by ajax:
Html code: only one list containing Dictionary data type is sent here
Because the data type sent is in the list dictionary format, we need to convert them into strings in advance. Otherwise, the data format received by the background program is not the type we want, therefore, JSON is required for ajax data transmission.
<! DOCTYPE html>
Views. py
# Coding: utf from django. shortcuts import render, HttpResponse, render_to_response import json # Create your views here. def Ajax (request): if request. method = 'post': print request. POST data = {'status':, 'msg ': 'request successfully', 'data': ['','', '']} return HttpResponse (json. dumps (data) else: return render_to_response ('app/ajax.html ')
Print data style:
The above is a demo of Django Ajax. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!