最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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 = '你搜索的內(nèi)容為: ' + 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 并搜索,結(jié)果如下所示:

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 記號,為表格處理結(jié)果預留位置。

表格后面還有一個{% 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 顯示結(jié)果如下:

完成以上實例后,我們的目錄結(jié)構(gòu)為:

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也有一些方法,如下表:

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

相關(guān)文章

  • python 如何上傳包到pypi

    python 如何上傳包到pypi

    這篇文章主要介紹了python 如何上傳包到pypi,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • 如何使用Python設置和讀取config.ini文件

    如何使用Python設置和讀取config.ini文件

    使用配置文件是一種常見的方法,而INI文件是一種簡單而常見的配置文件格式,在本文中,我將介紹如何使用Python設置和讀取INI格式的配置文件,需要的朋友可以參考下
    2024-03-03
  • python通過pil為png圖片填充上背景顏色的方法

    python通過pil為png圖片填充上背景顏色的方法

    這篇文章主要介紹了python通過pil為png圖片填充上背景顏色的方法,實例分析了Python使用pil模塊操作png圖片的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-03-03
  • jupyter閃退的解決方法及卸載與安裝教程

    jupyter閃退的解決方法及卸載與安裝教程

    如果Anaconda的Jupyter Notebook無法打開并且頻繁閃退,可能是由于一些配置問題或者軟件沖突引起的,本文就來介紹一下jupyter閃退的解決方法及卸載與安裝教程,感興趣的可以了解一下
    2023-11-11
  • python如何每天在指定時間段運行程序及關(guān)閉程序

    python如何每天在指定時間段運行程序及關(guān)閉程序

    這篇文章主要介紹了python如何每天在指定時間段運行程序及關(guān)閉程序問題,具有很好的參考價值,希望對大家有所幫助。
    2023-04-04
  • dpn網(wǎng)絡的pytorch實現(xiàn)方式

    dpn網(wǎng)絡的pytorch實現(xiàn)方式

    今天小編就為大家分享一篇dpn網(wǎng)絡的pytorch實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python zip()函數(shù)用法實例分析

    Python zip()函數(shù)用法實例分析

    這篇文章主要介紹了Python zip()函數(shù)用法,結(jié)合實例形式較為詳細的分析了Python zip()函數(shù)的功能、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下
    2018-03-03
  • Python3.5 創(chuàng)建文件的簡單實例

    Python3.5 創(chuàng)建文件的簡單實例

    下面小編就為大家分享一篇Python3.5 創(chuàng)建文件的簡單實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python必須了解的35個關(guān)鍵詞

    Python必須了解的35個關(guān)鍵詞

    這篇文章主要介紹了Python必須了解的35個關(guān)鍵詞,文中講解非常細致,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • 如何定義TensorFlow輸入節(jié)點

    如何定義TensorFlow輸入節(jié)點

    今天小編就為大家分享一篇如何定義TensorFlow輸入節(jié)點,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01

最新評論

栾城县| 汉源县| 阿荣旗| 富民县| 商都县| 常宁市| 江陵县| 阿坝县| 庆城县| 莲花县| 时尚| 双鸭山市| 舒城县| 泾源县| 松溪县| 广西| 敦化市| 砀山县| 桐城市| 上杭县| 南宫市| 石林| 永川市| 新营市| 斗六市| 峡江县| 雅安市| 右玉县| 双牌县| 新郑市| 亳州市| 江陵县| 宜川县| 平阳县| 高台县| 阳山县| 宽甸| 罗田县| 青浦区| 鸡西市| 钟山县|