Use Ajax notes under Django + jquery framework

Source: Internet
Author: User

In the past few days, I want to improve the python standard. I originally wanted to get a 24-point operation and output the result on the webpage. The result is rounded up and transferred to Django + jquery.
Finally, the interest has shifted to how Django uses the Ajax framework. Google has some web pages, which seem to be quite complicated. I remember it wasn't that complicated when I was playing PHP? I learned the example on the web page and it was still too troublesome. However, when Google's Ajax framework was found that jquery directly supports Ajax, it is definitely a different path to use other plugins to support Ajax. After a while, I finally found a solution and kept it as a note:

HTML file/template (index.html, adapted from http://hi.baidu.com/cpojutwoiebpvze/item/95e9c9be3b75c67d254b09d2 ):
<HTML xmlns = "http://www.w3.org/1999/xhtml" xmlns: V = "urn: Schemas-Microsoft-com: VML">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> googlejavascript </title>
<SCRIPT type = "text/JavaScript" src = "/static/jquery. js"> </SCRIPT>

<SCRIPT type = "text/JavaScript">
VaR XMLHTTP;
Function createxmlhttprequest (){
If (window. activexobject ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XMLHTTP = new XMLHttpRequest ();
}
}
Function startaa (){
// Createxmlhttprequest ();
// XMLHTTP. onreadystatechange = handlestatechange;
// Var I = 12;
// Var qurl = "/ajax/getdata /? TS = "+ new date (). gettime ();
// XMLHTTP. Open ("get", qurl, true );
// XMLHTTP. Send (null );
$. Ajax ({
Data:
"Ts =" + new date (). gettime (),
//{
// ID: 'ts = '+ new date (). gettime (), // ID,
// Value: 'ts = '+ new date (). gettime (), // ID,
//},
URL: "/ajax/getdata ",
Complete: function (XMLHttpRequest, textstatus ){
$ ('P'). Text (XMLHttpRequest. responsetext );
If (XMLHttpRequest. readystate = 4 ){
If (XMLHttpRequest. Status = 200 ){
Document. getelementbyid ("TXT"). value = XMLHttpRequest. responsetext;
Document. getelementbyid ("txtid"). value = XMLHttpRequest. responsetext;
}
}
},
Type: 'get ',

});
}
Function handlestatechange (){
If (XMLHTTP. readystate = 4 ){
If (XMLHTTP. Status = 200 ){
Document. getelementbyid ("TXT"). value = XMLHTTP. responsetext;
Document. getelementbyid ("txtid"). value = XMLHTTP. responsetext;
}
}
}
Function startccc (){
$. Get ("/ajax/getdata ",
"Ts =" + new date (). gettime (),
Function (data ){
$ ('P'). Text (data );
$ ("# TXT"). Val (data );
$ ("# Txtid"). Val (data );
// Another method, refer to "http://blog.csdn.net/yandt/article/details/5751098"
// $ ("# Txtid") [0]. value = data;
}
);
}
</SCRIPT>
</Head>
<Body>
<Div>
<Input id = "OK" value = "OK" type = "Submit" onclick = "startccc ();"/>
<Br>
<Input id = "txtid" type = "text" style = "width: 600px;" value = "{ggg}"/>
<Br>
<Textarea id = "TXT" style = "width: 600px; Height: 400px;" onkeyup = "startaa ();">... </textarea>
<Br>
</Div>
<P> </P>
</Body>
</Html>
It's much simpler and clearer to use jquery.

URL. py of mysite
(R' ^ ajax/', include ('ajax _ JQ. urls ')),

APP URLs. py:
(R' ^ index/', views. index ),
(R' ^ getdata/', views. getdata ),

Views. py:

Def index (request ):
Ggg = "hhhhhhhhh"
Return render_to_response ("index.html", locals ())

FIB = [0, 1]

Def get_fib_sequence ():
Global fib
FIB. append (FIB [-1] + fib [-2])
Return fib

Def getdata (request ):
Gg = "2000-9-9"
If request. Get:
Gg = "nihao"
If request. Request. has_key ("ts "):
Gg = request. request ["ts"]
Global fib
Get_fib_sequence ()
Ss = "--- Maid number: % d, % d" % (FIB [0], FIB [1])
Gg + = SS
Return httpresponse (gg)

Open chrome and enter 127.0.0.1: 8000/ajax/getdata. work OK.

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.