web前端基礎知識-(七)Django進階

經過上節課的學習,咱們已經對Django有了簡單的瞭解,如今來深刻了解下~html

1. 路由系統

1.1 單一路由對應python

1
url(r '^index$' , views.index),

1.2 基於正則的路由django

1
2
url(r '^index/(\d*)' , views.index),
url(r '^manage/(?P<name>\w*)/(?P<id>\d*)' , views.manage),
  • 找到urls.py文件,修改路由規則
1
2
3
4
5
6
7
8
from  django.conf.urls  import  url,include
from  django.contrib  import  admin
from  cmdb  import  views
  
urlpatterns  =  [
     url(r '^index' , views.index),
     url(r '^detail-(\d+).html/' , views.detail),
]
  • 在views.py文件建立對應方法
1
2
3
4
5
6
7
8
9
10
11
12
13
USER_DICT  =  {
     '1' :{ 'name' : 'root1' , 'email' : 'root@live.com' },
     '2' :{ 'name' : 'root2' , 'email' : 'root@live.com' },
     '3' :{ 'name' : 'root3' , 'email' : 'root@live.com' },
     '4' :{ 'name' : 'root4' , 'email' : 'root@live.com' },
}
  
def  index(request):
     return  render(request, "index.html" ,{ "user_dict" :USER_DICT})
  
def  detail(request,nid):   # nid指定的是(\d+)裏的內容
     detail_info  =  USER_DICT[nid]
     return  render(request,  "detail.html" , { "detail_info" : detail_info})

1.3 url分組app

在url.py增長對應路徑函數

1
2
3
4
5
6
7
8
from  django.conf.urls  import  url,include
from  django.contrib  import  admin
from  cmdb  import  views
  
urlpatterns  =  [
     url(r '^index' , views.index),
     url(r '^detail-(?P<nid>\d+)-(?P<uid>\d+).html/' , views.detail),<br>    # nid=\d+ uid=\d+
]

在views.py文件建立對應方法post

1
2
3
4
5
6
def  detail(request, * * kwargs):
     print (kwargs)         
     #{'nid': '4', 'uid': '3'}
     nid  =  kwargs.get( "nid" )
     detail_info  =  USER_DICT[nid]
     return  render(request,  "detail.html" , { "detail_info" : detail_info})

1.4 爲路由映射名稱學習

1
2
3
4
5
6
7
8
9
from  django.conf.urls  import  url,include
from  django.contrib  import  admin
from  cmdb  import  views
  
urlpatterns  =  [
     url(r '^asdfasdfasdf/' , views.index, name = 'i1' ),      #第一種方式i1
     url(r '^yug/(\d+)/(\d+)/' , views.index, name = 'i2' ),   #第二種方式i2
     url(r '^buy/(?P<pid>\d+)/(?P<nid>\d+)/' , views.index, name = 'i3' ),     #第三種方式i3
]

在templates目錄下的index.htmlui

1
2
3
4
5
6
7
8
9
10
11
12
<body>
{ #第一種方法i1       路徑asdfasdfasdf/#}
{ #<form action="{% url "i1" %}" method="post">#}
{ #第二種方法i2       路徑yug/1/2/#}
{ #<form action="{% url "i2" 1 2 %}" method="post">#}
{ #第三種方法i3       路徑buy/1/9//#}
<form action = "{% url " i3 " pid=1 nid=9 %}"  method = "post" >
     <p>< input   name = "user"  type = "text"  placeholder = "用戶名" / >< / p>
     <p>< input   name = "password"  type = "password"  placeholder = "密碼" / >< / p>
     <p>< input  type = "submit"  value = "提交" / >< / p>
< / form>
< / body>

1.5 根據app對路由分類url

主程序urls.py文件spa

1
2
3
4
5
6
from  django.conf.urls  import  url,include
from  django.contrib  import  admin
  
urlpatterns  =  [
     url(r '^monitor/' , include( 'monitor.urls' )),  #調整到monitor目錄中的urls.py文件
]

cmdb下的url.py文件

1
2
3
4
5
6
7
from  django.conf.urls  import  url
from  django.contrib  import  admin
from  monitor  import  views
#
urlpatterns  =  [
     url(r '^login' , views.login),
]

1.6 獲取當前URL

view.py中配置

1
2
3
4
def  index(request):
     print (request.path_info)     #獲取客戶端當前的訪問連接
     # / index
     return  render(request, "index.html" ,{ "user_dict" :USER_DICT})

在templates目錄下的index.html文件

1
2
3
4
5
<form action = "{{ request.path_info }}"  method = "post" >
     <p>< input   name = "user"  type = "text"  placeholder = "用戶名" / >< / p>
     <p>< input   name = "password"  type = "password"  placeholder = "密碼" / >< / p>
     <p>< input  type = "submit"  value = "提交" / >< / p>
< / form>

2. 視圖

2.1 獲取用戶請求數據

request.GET

request.POST

request.FILES 

其中,GET通常用於獲取/查詢 資源信息,而POST通常用於更新 資源信息 ; FILES用來獲取上傳文件;

2.2 checkbox等多選的內容

在templates目錄下建立login.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<! DOCTYPE  html>
< html  lang="en">
< head >
     < meta  charset="UTF-8">
     < title >Title</ title >
</ head >
< body >
     < form  action="/login" method="POST" >
         < p >
             男:< input  type="checkbox"  name="favor" value="11"/>
             女:< input  type="checkbox" name="favor" value="22"/>
             人妖:< input  type="checkbox" name="favor" value="33"/>
         </ p >
         < input  type="submit" value="提交"/>
     </ form >
</ body >
</ html >

修改views.py文件對錶單處理

1
2
3
4
5
6
7
8
9
10
11
def login(request):
     #checkbox  多選框
     if request.method == "POST":
         favor_list = request.POST.getlist("favor")      #getlist獲取多個值
         print(favor_list)           #多選框獲取到的是列表格式
         #['11', '22', '33']
         return render(request,"login.html")
     elif request.method == "GET":
         return render(request,"login.html")
     else:
         print("other")

2.3 上傳文件

1
2
3
4
文件對象 = reqeust.FILES.get()
文件對象.name
文件對象.size
文件對象.chunks()

 

在templates目錄下建立login.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<! DOCTYPE  html>
< html  lang="en">
< head >
     < meta  charset="UTF-8">
     < title >Title</ title >
</ head >
< body >
     < form  action="/login" method="POST" enctype="multipart/form-data">
         < p >
             < input  type="file" name="files"/>
         </ p >
         < input  type="submit" value="提交"/>
     </ form >
</ body >
</ html >

修改views.py文件對錶單處理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def login(request):
     #file 上傳文件
     if request.method == "POST":
         obj = request.FILES.get('files')       #用files獲取文件對象
         if obj:
             print(obj, type(obj), obj.name)
             # test.jpg < class  'django.core.files.uploadedfile.InMemoryUploadedFile'> test.jpg
             import os
             file_path = os.path.join('upload', obj.name)
             f = open(file_path, "wb")
             for item in obj.chunks():      #chunks表示全部的數據塊,是個迭代器
                 f.write(item)
             f.close()
         return render(request,"login.html")
     elif request.method == "GET":
         return render(request,"login.html")
     else:
         print("other")

2.4 FBV & CBV

2.4.1 FBV

1.在templates目錄下建立home.html文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<! DOCTYPE  html>
< html  lang="en">
< head >
     < meta  charset="UTF-8">
     < title >Title</ title >
</ head >
< body >
< form  action="/home/" method="POST">
     < p >
         < input  type="text" name="user" placeholder="用戶名"/>
     </ p >
     < p >
         < input  type="password" name="pwd" placeholder="密碼"/>
     </ p >
     < p >
         < input  type="submit" value="提交">
     </ p >
</ form >
</ body >
</ html >

2. 在urls.py文件增長home路徑

1
2
3
4
5
6
7
8
from django.conf.urls import url,include
from django.contrib import admin
from cmdb import views
  
urlpatterns = [
      # 固定語法
     url(r'^home/', views.Home.as_view()),
]

3. 在views.py文件建立函數Home

1
2
def home(request):
         return render(request,"home.html")

2.4.2 CBV

1. 在templates目錄下建立home.html文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<! DOCTYPE  html>
< html  lang="en">
< head >
     < meta  charset="UTF-8">
     < title >Title</ title >
</ head >
< body >
< form  action="/home/" method="POST">
     < p >
         < input  type="text" name="user" placeholder="用戶名"/>
     </ p >
     < p >
         < input  type="password" name="pwd" placeholder="密碼"/>
     </ p >
     < p >
         < input  type="submit" value="提交">
     </ p >
</ form >
</ body >
</ html >

2. 在urls.py文件增長home路徑

1
2
3
4
5
6
7
8
from django.conf.urls import url,include
from django.contrib import admin
from cmdb import views
  
urlpatterns = [
      # 固定語法
     url(r'^home/', views.Home.as_view()),
]

3. 在views.py文件建立類Home

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from django.views import View
  
class Home(View):
     # 先執行dispatch裏面的內容
     def dispatch(self,request, *args, **kwargs):
         print("before")
         # 調用父類中的dispatch
         result = super(Home,self).dispatch(request, *args, **kwargs)
         print("after")
         return result
  
     # 根據反射獲取用戶提交方式,執行get或post方法
     def get(self,request):
         print(request.method)
         return render(request,"home.html")
  
     def post(self,request):
         print(request.method)
         return render(request,"home.html")
相關文章
相關標籤/搜索