First, the design prototype
MTV model (django):
M: model layer (the models.py)
T: Templates
V: views
MVC model:
M: model layer (the models.py)
V: view layer (the views.py)
C: a controller (Controller) urls.py
django of MTV is MVC, not the same name, the design is different, but the same essence.
Two, front and rear ends data transmission format
urlencoded corresponding data format: name = & Jason password = 666 backend data acquisition: of request.POST PS; urlencoded Django will automatically parsed into coded data of request.POST FormData encoded form in the form of file transfer format rear acquiring data file format: request.FILES rear common key acquired data: of request.POST file application / json Ajax json format of the data transmitted to note the point encoding to be consistent with the data format
Three, Ajax asynchronous requests
1. What methods front end toward the rear end transmission request
Manually enter the URL of the browser window get request href attribute request to get a tag form in the form get / POST request (get request default) Ajax get / POST request
2.ajax Features:
Asynchronous submit
partial refresh
3.ajax basic grammar
Address filed (url) submission of (type) data (data) submitted by the callback function (Success) $ ( ' # D1 ' ) .click (function () { $ .ajax ({ // address submitted URL: ' / index / ' , // sUBMITTED type: ' POST ' , // submitted data data: { ' name ' : ' Jason ' , ' password ' : ' 123 ' }, // callback Success: function (Data) { // Data is submitted asynchronously received results returned Alert (Data) } }) })
4.ajax default data transmission
Encoding format is urlencoded
The front end of the transmission of data
You can not lie at home, what is the data format should tell others what format (encoded data to one correspondence)
$ ( '# d1') the Click (. function () { $ .ajax ({ url: '', // url parameter can not write, the default is to open the current page address of the type: 'POST' , contentType: 'the Application / JSON ' , Data: the JSON.stringify ({ ' name ':' Jason ',' Hobby ':' Study ' }), Success: function (Data) { {#alert (Data)} # {# $ ( ' # I3 ' ) .val (Data) #} } }) });
6.ajax transfer files
. $ ( '# D1') the Click ( function () { the let FormData = new new the FormData (); // the FormData objects can not only transfer document can also be transmitted on a common key formdata.append ( 'name', 'jason ' ); // get the file stored in the input frame // $ ( '# I1') [0] .files [0] formdata.append ( 'myfile', $ ( '# I1') [0] .files [0 ] ); $ .ajax ({ url: '' , of the type: 'POST' , the data: FormData, // ajax to send the file you need to modify two fixed parameters processData: false , // tell the browser not handle my data contentType:false, // Do not use any encoding, to use my own formdata encoding format, Django can automatically identify objects change formdata // callback Success: function (Data) { Alert (Data) } }) });
7.form form with ajax similarities and differences
1.form Forms does not support asynchronous submit partial refresh
2.form form transport json format data do not support
3.form form with ajax default encoding format for transmitting data is urlencoded