Django Ajax tutorial, djangoajax

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.