Django安裝及簡(jiǎn)單使用1.4

Django安裝及簡(jiǎn)單使用1.4

代碼都在github:
URL:https://github.com/njxshr/codes/tree/master/testdj

Django表單

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

  • HTTP 請(qǐng)求
    HTTP協(xié)議以"請(qǐng)求-回復(fù)"的方式工作??蛻舭l(fā)送請(qǐng)求時(shí),可以在請(qǐng)求中附加數(shù)據(jù)。服務(wù)器通過(guò)解析請(qǐng)求,就可以獲得客戶傳來(lái)的數(shù)據(jù),并根據(jù)URL來(lái)提供特定的服務(wù)。

GET 方法

  • 我們?cè)谥暗捻?xiàng)目中創(chuàng)建一個(gè) search.py 文件,用于接收用戶的請(qǐng)求:
    用來(lái)處理
# -*- coding: utf-8 -*-
import sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)

from django.http import HttpResponse
from django.shortcuts import render_to_response


# 表單
def search_form(request):
    return render_to_response('search_form.html')


# 接收請(qǐng)求數(shù)據(jù)
def search(request):
    request.encoding = 'utf-8'
    if 'q' in request.GET:
        message = '你搜索的內(nèi)容為: ' + request.GET['q']
    else:
        message = '你提交了空表單'
    return HttpResponse(message)

注意:如果出現(xiàn)編碼報(bào)錯(cuò)在search.py頭部加入

# -*- coding: utf-8 -*-
import sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)
  • 在模板目錄 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ī)則修改為如下形式:
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),
]

POST方法

  • 上面我們使用了GET方法。視圖顯示和請(qǐng)求處理分成兩個(gè)函數(shù)處理。
    提交數(shù)據(jù)時(shí)更常用POST方法。我們下面使用該方法,并用一個(gè)URL和處理函數(shù),同時(shí)顯示視圖和處理請(qǐng)求。

  • 我們?cè)趖mplate 創(chuàng)建 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>

在模板的末尾,我們?cè)黾右粋€(gè) rlt 記號(hào),為表格處理結(jié)果預(yù)留位置。

表格后面還有一個(gè){% csrf_token %}的標(biāo)簽。csrf 全稱是 Cross Site Request Forgery。這是Django提供的防止偽裝提交請(qǐng)求的功能。POST 方法提交的表格,必須有此標(biāo)簽。

  • 在HelloWorld目錄下新建 search2.py 文件并使用 search_post 函數(shù)來(lái)處理 POST 請(qǐng)求:
# -*- coding: utf-8 -*-
 
from django.shortcuts import render
from django.views.decorators import csrf
 
# 接收POST請(qǐng)求數(shù)據(jù)
def search_post(request):
    ctx ={}
    if request.POST:
        ctx['rlt'] = request.POST['q']
    return render(request, "post.html", ctx)
  • urls.py 規(guī)則修改為如下形式:
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),
]
image.png
目錄結(jié)構(gòu)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 當(dāng)我抽完血,看見手機(jī)上顯示母親打的兩個(gè)未接電話。 我拿著手機(jī),努力平復(fù)心情,撥通了電話。 "阿妹,"電話里傳來(lái)母親...
    爭(zhēng)做一棵懸崖邊的樹閱讀 338評(píng)論 0 0
  • 門前有兩條路 一條寬敞 參天的古木勾勒出藍(lán)天一線 一條明亮 叢生的灌木掩映著暗處水洼 兩條路都安靜地指著遠(yuǎn)方 只有...
    阿念和阿書閱讀 203評(píng)論 0 1
  • 文/箽四四(原創(chuàng)) 01 早上送你上學(xué)了,你不哭不鬧,跟我揮手再見,那一刻,突然覺得傷心,覺得好對(duì)不起你。 不知什...
    箽四四閱讀 732評(píng)論 42 46
  • 微風(fēng)輕柔 喝了點(diǎn)小酒 我坐在體育中心旁的小石凳 寧?kù)o而安詳 對(duì)面是閃著微黃燈亮的樓宇 溫柔而美麗 這就是家的感覺 ...
    黃海佳閱讀 276評(píng)論 2 0

友情鏈接更多精彩內(nèi)容