Django Views模組詳解

2022-02-09 04:12:45 字數 4685 閱讀 3590

http請求: httprequest http響應: httpresponse 所在位置 django.http

請求頁面的全路徑,不包含網域名稱---完整的請求路徑,不包括域 例: "/music/bands/the_beatles/"。

請求中使用的http方法的字串表示。全大寫表示。例如

get請求物件,包含http get引數的類字典物件,詳細檢視 querydict文件

post請求物件,包含http get引數的類字典物件,詳細檢視 querydict文件,

伺服器收到空的post請求的情況也是可能發生的,也就是說,表單from通過http post

方法提交請求,單是表單中可以沒有資料。因此,不能使用御酒 if request.post來判斷

是否使用http post方法,應該使用if request.method == "post" (參見本表的method

屬性)。

注意: post不包含file-upload(檔案上傳)資訊,請看 files

httprequest.request  django 1.8之後被廢棄了

為了方便,該屬性是post和get屬性的集合體,但是有特殊性,先

找到post屬性,然後在查詢get屬性。借鑑php`s$_request。

例如,如果get=和post=,冊 request['name']

的值為"jhon",request["age"]的值是'34'.

強烈建議使用get and post,因為這兩個屬性更加顯示化,寫出來的**更易於

理解

httprequest.cookies

包含所有cookies的標準 python字典物件,keys 和values都是字串 參見12章 有關於

cookies更詳細的講解

包含所有上傳檔案的類字典物件。files中每個key都是標籤中的name屬性的值,files中每個value同事也是乙個標準的python字典物件,包

含下面三個keys:

1. filename 上傳檔名,用python字串表示

2. content-type 上傳檔案的content type

3. content 上傳檔案的原始內容

注意: 只有在請求方法是post,並且請求頁面中有enctype="multipart/frm-data"

屬性時files才擁有資料。否則files是乙個空字典

包含所有可用http頭部資訊的字典。例如:

1. content_length

2. content_type

3. query_string:未解析的原始查詢字串

4. remote_addr:客戶端主機名

5. server_name:伺服器主機名

6. server_port :伺服器端口

meta中寫著頭加上字首http_ key 例如:

3. http_post 客戶傳送的http主機頭資訊

4. http_referer:referring頁面

5. http_user_agent:客戶端的user-agent字串

6. http_x_bender:x-bender頭部資訊

是from django.contrib.auth.models.user物件,代表當前登入的使用者,如果訪問使用者當前沒有

登入,user將被初始化為from django.contrib.auth.models.anonymoususer的例項。

你可以通過user的is_authenticated()方法來辨別使用者是否登入

if request.user.is_authenticated():

do something for logged-in users

else:

do something for anonymous users

只有啟用django中的 a uthenticationmiddleware時該屬性才可用

唯一可讀寫的屬性,代表當前回話的字典物件,只有啟用django中的

session支援是該屬性才可用

-raw_post_data

原始http post資料,未解析過。改機處理時會有用。

- htpprequest物件的方法部分

1. get_full_path()

返回包含查詢字串的請求路徑。例如,

"/music/bands/the_beatles/?print=true"。

2. querydict物件

get() 如果key對應多個value,get()返回最後乙個value。

在httprequest物件中,get和post屬性時 django.http.querydict類的例項。

對於httprequest物件來說 django自動建立,但是 httpresponse物件必須我們自己建立 每個view方法必須返回乙個httpresponse物件。httpresponse類在django.http.httpresponse.render方法包裝的也是乙個response物件。

詳見:

render rander_to_response redirect 跳轉的方法 更多詳見:

locals() 可以直接將函式中所有變數全部傳遞給模板
body

body>*:first-child

body>*:last-child

p, blockquote, ul, ol, dl, table, pre

h1, h2, h3, h4, h5, h6

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code

h1 h2

h3 h4

h5 h6

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p

a a:hover

ul, ol

ul li>:first-child, ol li>:first-child, ul li ul:first-of-type, ol li ol:first-of-type, ul li ol:first-of-type, ol li ul:first-of-type

ul ul, ul ol, ol ol, ol ul

dl dl dt

dl dt:first-child

dl dt>:first-child

dl dt>:last-child

dl dd

dl dd>:first-child

dl dd>:last-child

pre, code, tt

code, tt

pre>code

pre

pre code, pre tt

kbd

blockquote

blockquote>:first-child

blockquote>:last-child

hr table th

table th, table td

table tr

table tr:nth-child(2n)

img

模組之shutil模組模組詳解

shutil模組是高階的 檔案 資料夾 壓縮包 處理模組 shutil.copyfileobj fsrc,fdst length 將檔案內容拷貝到另乙個檔案中 shutil.copyfile src,dst 拷貝檔案 shutil.copymode src,dst 僅拷貝許可權。內容 組 使用者均不...

模組之ConfigParser模組詳解

在程式中使用配置檔案來靈活的配置一些引數是一件很常見的事情,配置檔案的解析並不複雜,在python裡更是如此,在官方發布的庫中就包含有做這件事情的庫,那就是configparser,這裡簡單的做一些介紹。configparser解析的配置檔案的格式為.ini的配置檔案格式,就是檔案中由多個secti...

模組之random模組詳解

random是python內建模組,想要使用該模組,第一步需要進行匯入,下面介紹幾種random常用的函式 import random print random.random 大於0且小於1之間的小數 print random.randint 1,3 1,3 大於等於1且小於等於3之間的整數 pri...