Django Request life cycle:
-->url (matching), view function, return user string
-->url (matching), view function, open an HTML file, read the content
Create a Django Project
Django-admin Startproject MySite
CD MySite
Python manage.py Startapp CMDB
MySite
MySite
--Configuration file
-url.py
-settings.py
CD MySite
Cmdb
-views.py
-admin.py
-models.py # Creating a database table
3. Configuration
Template path
Static file path
Note #csrf
4. Writing Procedures
A.url.py Write Correspondence Relation/index/-func
B. views.py
def func (Request): # request contains all the requested data
... Business code
Return HttpResponse (' string ')
return render (Request, ' index.html ', {'})
Return redirect (' URL ')
C. Template language
{% for item in%}
One, routing system, URL
Second, the View
Request. GET
Request. POST
Request. FILES
#checkbox等多选的内容 request. Post.getlist ()
#上传文件
obj = Request. Files.get (' name ')
Obj.name
f = open (obj.name,mode= ' WB ')
For item in Obj.chunks ():
F.write (item)
F.close ()
views.py
From django.shortcuts import render,httpresponse,redirect# Create your views Here.def index (Request): Return Httprespon Se (' Index ') ' Def login (Request): if Request.method = = ' GET ': Return render (Request, ' login.html ') elif reques T.method = = ' POST ': u = Request. Post.get (' user ') p = Request. Post.get (' pwd ') if u = = ' Alex ' and p = = ' 123 ': Return redirect ('/index/') Else:return Redirect (' login.html ') Else: # put,delete,head,option ... return redirect ('/index/') ' Def login (Request) : if Request.method = = ' GET ': Return render (Request, ' login.html ') elif Request.method = = ' POST ': v = r Equest. Post.get (' Gender ') print (v) f = Request. Post.getlist (' favor ') # Gets the multi-person value of the checkbox print (f) FFF = Request. Files.get (' file ') # default only gets the file name content = Request. Files.get (' file ') # Fetch file contents print (Fff,type (FFF)) return render (Request, ' login.html ') Else: # Put,del EtE,head,option. Return redirect ('/index/')
#上传文件的话, form label to do special treatment enctype= "Multipart/form-data"
<! DOCTYPE html>Login.html
Django Routing Rules
1, the URL based on the regular
Create index.html, detail.html files under the Templates directory
<! DOCTYPE html>"en">"UTF-8"> <title>Title</title>{% forK,iteminchUser_dict.items%} <li><a target="_blank"href="/detail-{{k}}.html">{{item.name}}</a></li>{% ENDFOR%}</body>index.html
index.html<! DOCTYPE html>"en">" UTF-8"> <title>Title</title>detail.html
detail.htmlAdd the corresponding path in the urls.py file
From django.conf.urls import url,includefrom django.contrib import adminfrom CMDB import views urlpatterns = [ url (r ') ^index ', views.index), url (r ' ^detail-(\d+). html/', Views.detail),]
Python development "Django": Routing rules