欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django框架中表單的用法

 更新時間:2022年06月10日 15:50:48   作者:springsnow  
這篇文章介紹了Django框架中表單的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

HTML表單是網(wǎng)站交互性的經(jīng)典方式。 本章將介紹如何用Django對用戶提交的表單數(shù)據(jù)進行處理。

一、HTTP 請求

HTTP協(xié)議以"請求-回復"的方式工作??蛻舭l(fā)送請求時,可以在請求中附加數(shù)據(jù)。服務器通過解析請求,就可以獲得客戶傳來的數(shù)據(jù),并根據(jù)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')
 
# 接收請求數(shù)據(jù)
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方法。視圖顯示和請求處理分成兩個函數(shù)處理。

提交數(shù)據(jù)時更常用POST方法。我們下面使用該方法,并用一個URL和處理函數(shù),同時顯示視圖和處理請求。

我們在 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 函數(shù)來處理 POST 請求:

/HelloWorld/HelloWorld/search2.py 文件代碼:

# -*- coding: utf-8 -*-
 
from django.shortcuts import render
from django.views.decorators import csrf
 
# 接收POST請求數(shù)據(jù)
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 函數(shù)的第一個參數(shù)是一個 HttpRequest 對象,就像下面這個 hello() 函數(shù):

from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello world")

HttpRequest對象包含當前請求URL的一些信息:

Request對象也有一些有用的方法:

QueryDict對象

在HttpRequest對象中, GET和POST屬性是django.http.QueryDict類的實例。

QueryDict類似字典的自定義類,用來處理單鍵對應多值的情況。

QueryDict實現(xiàn)所有標準的詞典方法。還包括一些特有的方法:

此外, QueryDict也有一些方法,如下表:

到此這篇關于Django框架中表單用法的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論