Catalogue
one, Django URL Application
second, Django View Application
third, Django Template Application
Iv. Django's ORM Application
Add:
Supplement One: Getlis
Application Scenario: The current side of the data sent at multiple times, the background if you also get data with get, then there will be a problem, so there is a getlist method for data Acquisition.
Test Case: When there is a multi-select checkbox at the current end, then we need to use GetList to get the Data.
urlpatterns = [ url (r'^admin/', admin.site.urls), url (r') ^getlist', views.getlist),]
urls.py
def getlist (request): if request.method = = " get " : ret Urn render (request, " getlist.html " ) else : che Ckbox = Request. Post.getlist ( ' interest " ) print return httpresponse (checkbox)
views.py
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Title</title></Head><Body><formAction= "/getlist"Method= "post"> <Div>Hobbies Survey Questionnaire</Div> <span>Man</span> <inputtype= "checkbox"name= "interest"value= "1"> <span>Woman</span> <inputtype= "checkbox"name= "interest"value= "2"> <span>No requirement</span> <inputtype= "checkbox"name= "interest"value= "3"> <inputtype= "submit"value= "submit"></form></Body></HTML>
HTML code
View Results:
Same as the use of the SELECT tag
Add two: Upload Files
I learned it. get data from the foreground (single data, multiple data), How do I get a file from the foreground? Here's to say that you can't use request to get a file. Post to get it,
Need to use Request. Files for data Acquisition.
Write a case for uploading a file:
urlpatterns = [ url (r'^admin/', admin.site.urls), url (r') ^upload/', views.upload),]
urls.py
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Title</title></Head><Body>{# enctype= "multipart/form-data" If the submitted data is a file, then you have to write this sentence in the form form #}<formAction= "/upload/"Method= "POST"enctype= "multipart/form-data"> <P> <inputtype= "file"name= "file"/> </P> <inputtype= "submit"value= "submit"/> </form></Body></HTML>
HTML code
fromDjango.shortcutsImportRender,httpresponse fromDjango.viewsImportView#Create your views here.defUpload (request):ifRequest.method = ='GET': returnRender (request,'upload.html') elifRequest.method = ='POST': #get the file sent to the foreground, if directly print obj, will print the file name of the uploaded files; #in fact, what he received is not a file name, you can print some types of obj, and the result is a class objectobj = Request. Files.get ('file') ImportOS#add a upload directory in front of obj.name the last directory path is upload/uploaded file name #here Obj.name is called the name property, the property value is the uploaded file nameFile_pat = Os.path.join ('Upload', Obj.name) F= Open (file_pat,'WB') Print(os.path)#loop to get uploaded data forIinchobj.chunks (): f.write (i) f.close ()returnRender (request,'upload.html') Else: returnRender (request,'upload.html')
views.py Master Logicthe Django URL app
Django's View App
FBV (funaction Base View)----> The url-routing relationship we learned before is a request to be matched by a url, see matching to that method directly to the views.py code body inside the corresponding hair method lookup, and then the logical processing.
CBV (class Base view)-----> now say a CBV pattern, and when the request comes in, it is also matched by the url, this time it is not a method, but a class.
So how does CBV work? Look at the Code:
urlpatterns = [ url (r'^admin/', admin.site.urls), #FBV url (r'^home/', Views. Home.as_view ()), #CBV]
urls.py
fromDjango.shortcutsImportRender,httpresponse fromDjango.viewsImportView#introducing a Django inside View module#Define this type ofclassHome (View):#here is the use of class mapping to operate, you can look at the parent view there is a dispatch method. When a GET request is used #The following get methods are executed ... defGet (self,request):Print(REQUEST.METHOD)returnRender (request,'home.html') defPost (self,request):Print(REQUEST.METHOD)returnRender (request,'home.html')
views.py
Django has provided us with so many requests, so let's comb the process:
That is, the dispatch method in the parent class must be executed before executing our defined method, so we can do something about it. Think of the "adorner" and see the Code:
classHome (View):defDispatch (self,request,*args,**kwargs):Print('befor') Result= Super (home,self). Dispatch (request,*args,**Kwargs)Print(' after') returnresult#here is the use of class mapping to operate, you can look at the parent view there is a dispatch method. When a GET request is used #The following get methods are executed ... defGet (self,request):Print(REQUEST.METHOD)returnRender (request,'home.html') defPost (self,request):Print(REQUEST.METHOD)returnRender (request,'home.html')
views.py
Is the befor and after printed here similar to the decorator we learned before?
Let's comb the current process:
The first step: the browser sends the packet to the Server's URLs routing system in any way.
The second step: URLs routing system to match the request url, found matching to a class, the request to the corresponding class
Step three: the corresponding class finds the dispatch method within itself, first executing its own added block of code
Fourth step: How to obtain the request from the integrated Father
Fifth step: What is the way the parent class tells the subclass to request
Sixth step: The subclass finds the function of its own internal counterpart and executes the code block and returns the data to the browser
1
Life is short, I use python--Day19 the Django Framework URL Routing system, View application, template application, Django ORM Application