Django 表单
HTML表單是網站交互性的經典方式。 本章將介紹如何用Django對用戶提交的表單數據進行處理。
- *
HTTP 請求
HTTP協議以"請求-回復"的方式工作??蛻舭l送請求時,可以在請求中附加數據。服務器通過解析請求,就可以獲得客戶傳來的數據,并根據URL來提供特定的服務。
GET 方法
我們在之前的項目中創建一個 search.py 文件,用于接收用戶的請求:
/HelloWorld/HelloWorld/search.py 文件代碼:
# -*- coding: utf-8 -*-from django.http import HttpResponse from django.shortcuts import render_to_response# 表單 def search_form(request):return render_to_response('search_form.html')# 接收請求數據 def search(request): request.encoding='utf-8'if 'q' in request.GET:message = '你搜索的內容為: ' + request.GET['q'].encode('utf-8')else:message = '你提交了空表單'return HttpResponse(message)在模板目錄 templates 中添加 search_form.html 表單:
/HelloWorld/templates/search_form.html 文件代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>www.hiekay.com</title> </head> <body><form action="/search" method="get"><input type="text" name="q"><input type="submit" value="搜索"></form> </body> </html>urls.py 規則修改為如下形式:
/HelloWorld/HelloWorld/urls.py 文件代碼:
from django.conf.urls import url from . import view,testdb,searchurlpatterns = [url(r'^hello$', view.hello),url(r'^testdb$', testdb.testdb),url(r'^search-form$', search.search_form),url(r'^search$', search.search), ]訪問地址 http://127.0.0.1:8000/search-form 并搜索,結果如下所示:
POST 方法
上面我們使用了GET方法。視圖顯示和請求處理分成兩個函數處理。
提交數據時更常用POST方法。我們下面使用該方法,并用一個URL和處理函數,同時顯示視圖和處理請求。
我們在 templates 創建 post.html:
/HelloWorld/templates/post.html 文件代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>www.hiekay.com</title> </head> <body><form action="/search-post" method="post">{% csrf_token %}<input type="text" name="q"><input type="submit" value="Submit"></form><p>{{ rlt }}</p> </body> </html>在模板的末尾,我們增加一個 rlt 記號,為表格處理結果預留位置。
表格后面還有一個{% csrf_token %}的標簽。csrf 全稱是 Cross Site Request Forgery。這是Django提供的防止偽裝提交請求的功能。POST 方法提交的表格,必須有此標簽。
在HelloWorld目錄下新建 search2.py 文件并使用 search_post 函數來處理 POST 請求:
/HelloWorld/HelloWorld/search2.py 文件代碼:
# -*- coding: utf-8 -*-from django.shortcuts import render from django.views.decorators import csrf# 接收POST請求數據 def search_post(request):ctx ={}if request.POST:ctx['rlt'] = request.POST['q']return render(request, "post.html", ctx)urls.py 規則修改為如下形式:
/HelloWorld/HelloWorld/urls.py 文件代碼:
from django.conf.urls import url from . import view,testdb,search,search2urlpatterns = [url(r'^hello$', view.hello),url(r'^testdb$', testdb.testdb),url(r'^search-form$', search.search_form),url(r'^search$', search.search),url(r'^search-post$', search2.search_post), ]訪問 http://127.0.0.1:8000/search-post 顯示結果如下:
完成以上實例后,我們的目錄結構為:
HelloWorld |-- HelloWorld | |-- __init__.py | |-- __init__.pyc | |-- search.py | |-- search.pyc | |-- search2.py | |-- search2.pyc | |-- settings.py | |-- settings.pyc | |-- testdb.py | |-- testdb.pyc | |-- urls.py | |-- urls.pyc | |-- view.py | |-- view.pyc | |-- wsgi.py | `-- wsgi.pyc |-- TestModel | |-- __init__.py | |-- __init__.pyc | |-- admin.py | |-- admin.pyc | |-- apps.py | |-- migrations | | |-- 0001_initial.py | | |-- 0001_initial.pyc | | |-- __init__.py | | `-- __init__.pyc | |-- models.py | |-- models.pyc | |-- tests.py | `-- views.py |-- db.sqlite3 |-- manage.py `-- templates|-- base.html|-- hello.html|-- post.html`-- search_form.html- *
Request 對象
每個 view 函數的第一個參數是一個 HttpRequest 對象,就像下面這個 hello() 函數:
from django.http import HttpResponsedef hello(request):return HttpResponse("Hello world")HttpRequest對象包含當前請求URL的一些信息:
| path | 請求頁面的全路徑,不包括域名—例如, "/hello/"。 |
| method | 請求中使用的HTTP方法的字符串表示。全大寫表示。例如:if?request.method?==?'GET': do_something() elif?request.method?==?'POST': ????do_something_else() |
| GET | 包含所有HTTP GET參數的類字典對象。參見QueryDict 文檔。 |
| POST | 包含所有HTTP POST參數的類字典對象。參見QueryDict 文檔。 服務器收到空的POST請求的情況也是有可能發生的。 也就是說,表單form通過HTTP POST方法提交請求,但是表單中可以沒有數據。 因此,不能使用語句if request.POST來判斷是否使用HTTP POST方法;應該使用if request.method == "POST" (參見本表的method屬性)。注意: POST不包括file-upload信息。參見FILES屬性。 |
| REQUEST | 為了方便,該屬性是POST和GET屬性的集合體,但是有特殊性,先查找POST屬性,然后再查找GET屬性。借鑒PHP's $_REQUEST。 例如,如果GET = {"name": "john"} 和POST = {"age": '34'},則 REQUEST["name"] 的值是"john", REQUEST["age"]的值是"34".強烈建議使用GET and POST,因為這兩個屬性更加顯式化,寫出的代碼也更易理解。 |
| COOKIES | 包含所有cookies的標準Python字典對象。Keys和values都是字符串。 |
| FILES | 包含所有上傳文件的類字典對象。FILES中的每個Key都是標簽中name屬性的值. FILES中的每個value 同時也是一個標準Python字典對象,包含下面三個Keys: filename: 上傳文件名,用Python字符串表示 content-type: 上傳文件的Content type * content: 上傳文件的原始內容 注意:只有在請求方法是POST,并且請求頁面中有enctype="multipart/form-data"屬性時FILES才擁有數據。否則,FILES 是一個空字典。 |
| META | 包含所有可用HTTP頭部信息的字典。 例如: CONTENT_LENGTH CONTENT_TYPE QUERY_STRING: 未解析的原始查詢字符串 REMOTE_ADDR: 客戶端IP地址 REMOTE_HOST: 客戶端主機名 SERVER_NAME: 服務器主機名 SERVER_PORT: 服務器端口 META 中這些頭加上前綴HTTP_最為Key, 例如: HTTP_ACCEPT_ENCODING HTTP_ACCEPT_LANGUAGE HTTP_HOST: 客戶發送的HTTP主機頭信息 HTTP_REFERER: referring頁 HTTP_USER_AGENT: 客戶端的user-agent字符串 * HTTP_X_BENDER: X-Bender頭信息 |
| user | 是一個django.contrib.auth.models.User 對象,代表當前登錄的用戶。 如果訪問用戶當前沒有登錄,user將被初始化為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中的AuthenticationMiddleware時該屬性才可用 |
| session | 唯一可讀寫的屬性,代表當前會話的字典對象。只有激活Django中的session支持時該屬性才可用。 |
| raw_post_data | 原始HTTP POST數據,未解析過。 高級處理時會有用處。 |
Request對象也有一些有用的方法:
| __getitem__(key) | 返回GET/POST的鍵值,先取POST,后取GET。如果鍵不存在拋出 KeyError。?這是我們可以使用字典語法訪問HttpRequest對象。?例如,request["foo"]等同于先request.POST["foo"] 然后 request.GET["foo"]的操作。 |
| has_key() | 檢查request.GET or request.POST中是否包含參數指定的Key。 |
| get_full_path() | 返回包含查詢字符串的請求路徑。例如, "/music/bands/the_beatles/?print=true" |
| is_secure() | 如果請求是安全的,返回True,就是說,發出的是HTTPS請求。 |
QueryDict對象
在HttpRequest對象中, GET和POST屬性是django.http.QueryDict類的實例。
QueryDict類似字典的自定義類,用來處理單鍵對應多值的情況。
QueryDict實現所有標準的詞典方法。還包括一些特有的方法:
| getitem | 和標準字典的處理有一點不同,就是,如果Key對應多個Value,__getitem__()返回最后一個value。 |
| setitem | 設置參數指定key的value列表(一個Python list)。注意:它只能在一個mutable QueryDict 對象上被調用(就是通過copy()產生的一個QueryDict對象的拷貝). |
| get() | 如果key對應多個value,get()返回最后一個value。 |
| update() | 參數可以是QueryDict,也可以是標準字典。和標準字典的update方法不同,該方法添加字典 items,而不是替換它們: >>> q = QueryDict('a=1') >>> q = q.copy() # to make it mutable >>> q.update({'a': '2'}) >>> q.getlist('a') ['1', '2'] >>> q['a'] # returns the last ['2'] |
| items() | 和標準字典的items()方法有一點不同,該方法使用單值邏輯的__getitem__(): >>> q = QueryDict('a=1&a=2&a=3') >>> q.items() [('a', '3')] |
| values() | 和標準字典的values()方法有一點不同,該方法使用單值邏輯的__getitem__(): |
此外, QueryDict也有一些方法,如下表:
| copy() | 返回對象的拷貝,內部實現是用Python標準庫的copy.deepcopy()。該拷貝是mutable(可更改的) — 就是說,可以更改該拷貝的值。 |
| getlist(key) | 返回和參數key對應的所有值,作為一個Python list返回。如果key不存在,則返回空list。 It's guaranteed to return a list of some sort.. |
| setlist(key,list_) | 設置key的值為list_ (unlike __setitem__()). |
| appendlist(key,item) | 添加item到和key關聯的內部list. |
| setlistdefault(key,list) | 和setdefault有一點不同,它接受list而不是單個value作為參數。 |
| lists() | 和items()有一點不同, 它會返回key的所有值,作為一個list, 例如: >>>> q = QueryDict('a=1&a=2&a=3') >>> q.lists() [('a', ['1', '2', '3'])] |
| urlencode() | 返回一個以查詢字符串格式進行格式化后的字符串(e.g., "a=2&b=3&b=5"). |
總結
- 上一篇: iPhone7Plus从iOS13.3升
- 下一篇: 华为路由WS5200四核版测评-华为路由