網站首頁 編程語言 正文
HTML表單是網站交互性的經典方式。 本章將介紹如何用Django對用戶提交的表單數據進行處理。
一、HTTP 請求
HTTP協議以"請求-回復"的方式工作。客戶發(fā)送請求時,可以在請求中附加數據。服務器通過解析請求,就可以獲得客戶傳來的數據,并根據URL來提供特定的服務。
1、GET 方法
我們在之前的項目中創(chuàng)建一個 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 and request.GET['q']:
message = '你搜索的內容為: ' + request.GET['q']
else:
message = '你提交了空表單'
return HttpResponse(message)
在模板目錄 templates 中添加 search_form.html 表單:
/HelloWorld/templates/search_form.html 文件代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<form action="/search" method="get">
<input type="text" name="q">
<input type="submit" value="搜索">
</form>
</body>
</html>
urls.py 規(guī)則修改為如下形式:
/HelloWorld/HelloWorld/urls.py 文件代碼:
from django.conf.urls import url
from . import view,testdb,search
urlpatterns = [
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 并搜索,結果如下所示:
2、POST 方法
上面我們使用了GET方法。視圖顯示和請求處理分成兩個函數處理。
提交數據時更常用POST方法。我們下面使用該方法,并用一個URL和處理函數,同時顯示視圖和處理請求。
我們在 templates 創(chuàng)建 post.html:
/HelloWorld/templates/post.html 文件代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.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 規(guī)則修改為如下形式:
/HelloWorld/HelloWorld/urls.py 文件代碼:
from django.conf.urls import url
from . import view,testdb,search,search2
urlpatterns = [
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 HttpResponse
def hello(request):
return HttpResponse("Hello world")
HttpRequest對象包含當前請求URL的一些信息:
Request對象也有一些有用的方法:
QueryDict對象
在HttpRequest對象中, GET和POST屬性是django.http.QueryDict類的實例。
QueryDict類似字典的自定義類,用來處理單鍵對應多值的情況。
QueryDict實現所有標準的詞典方法。還包括一些特有的方法:
此外, QueryDict也有一些方法,如下表:
原文鏈接:https://www.cnblogs.com/springsnow/p/13170138.html
相關推薦
- 2022-09-26 數據庫多表聯查的方式
- 2022-05-29 Docker鏡像與容器的導入導出操作實踐_docker
- 2022-04-16 WPF框架Prism中區(qū)域Region用法介紹_實用技巧
- 2022-03-29 C#算法之兩數之和_C#教程
- 2022-07-22 如何處理SQL Server中附加數據庫時出現的錯誤
- 2023-11-16 當pytorch找不到 CUDA 時,如何修復錯誤:版本 libcublasLt.so.11 未在帶
- 2022-05-15 Python?中的集合和字典_python
- 2022-06-02 EasyX實現自由落體小球_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支