国产aaaa级全身裸体精油片_337p人体粉嫩久久久红粉影视_一区中文字幕在线观看_国产亚洲精品一区二区_欧美裸体男粗大1609_午夜亚洲激情电影av_黄色小说入口_日本精品久久久久中文字幕_少妇思春三a级_亚洲视频自拍偷拍

首頁 > 行業(yè)資訊 > Django教程-表單

Django教程-表單

時間:2023-07-27 來源: 瀏覽:

Django教程-表單

點擊關(guān)注 Python架構(gòu)師
Python架構(gòu)師

gh_1d7504e4dee1

回復:python,領(lǐng)取Python面試題。分享Python教程,Python架構(gòu)師教程,Python爬蟲,Python編程視頻,Python腳本,Pycharm教程,Python微服務(wù)架構(gòu),Python分布式架構(gòu),Pycharm注冊碼。

收錄于合集
#django 5
#Django開源項目 15
#Django教程 14
#python web教程 23
#python教程 113
整理: python架構(gòu)師

Django 提供了一個 Form 類,用于創(chuàng)建 HTML 表單。它描述了一種形式以及它如何工作和顯示。

它類似于使用Model創(chuàng)建表單的ModelForm類,但它不需要Model。

表單類的每個字段都映射到 HTML 表單<input>元素,每個字段本身就是一個類,它管理表單數(shù)據(jù)并在提交表單時執(zhí)行驗證。

讓我們看一個示例,其中我們也創(chuàng)建了一些字段。

from django import forms class StudentForm (forms.Form) : firstname = forms.CharField(label= "Enter first name" ,max_length= 50 ) lastname = forms.CharField(label= "Enter last name" , max_length = 100 )

 

創(chuàng)建一個 StudentForm,其中包含兩個 CharField 類型的字段。Charfield 是一個類,用于在表單中創(chuàng)建 HTML 文本輸入組件。

label用于設(shè)置組件的HTML標簽,max_length設(shè)置輸入值的長度。

點擊領(lǐng)取Python面試題手冊

Python從入門到進階知識手冊

渲染后,它會向瀏覽器生成以下 HTML。

< label for = "id_firstname" > Enter first name: </ label > < input type = "text" name = "firstname" required maxlength = "50" id = "id_firstname" /> < label for = "id_lastname" > Enter last name: </ label > < input type = "text" name = "lastname" required maxlength = "100" id = "id_lastname" />  

注意:Django Form 不包含 <form> 標簽或提交按鈕。我們必須在模板中自己提供這些內(nèi)容。

下表給出了常用字段及其詳細信息。

名稱 HTML 輸入 空值
BooleanField class BooleanField(**kwargs) 復選框輸入 False
CharField class CharField(**kwargs) 文本輸入 無論您提供什么作為empty_value。
ChoiceField class ChoiceField(**kwargs) 選擇 ’’(空字符串)
DateField class DateField(**kwargs) 日期輸入 None
DateTimeField class DateTimeField(**kwargs) 日期時間輸入 None
DecimalField class DecimalField(**kwargs) 數(shù)字輸入 None
EmailField class EmailField(**kwargs) 電子郵件輸入 ’’(空字符串)
FileField class FileField(**kwargs) 可清除文件輸入 None
ImageField class ImageField(**kwargs) 可清除文件輸入 None

讓我們看一下在 Django Form 類的幫助下創(chuàng)建 HTML 表單的完整示例。

在 Django 中構(gòu)建表單

假設(shè)我們要創(chuàng)建一個表單來獲取學生信息,請使用以下代碼。

from django import forms class StudentForm (forms.Form) : firstname = forms.CharField(label= "Enter first name" ,max_length= 50 ) lastname = forms.CharField(label= "Enter last name" , max_length = 100 )

將此代碼放入forms.py文件中。

在 Django 中實例化表單

現(xiàn)在,我們需要在views.py文件中實例化表單。請看下面的代碼。

// 視圖.py

from django.shortcuts import render from myapp.form import StudentForm def index (request) : student = StudentForm() return render(request, "index.html" ,{ ’form’ :student})

將表單的上下文傳遞到索引模板中,如下所示:

// 索引.html

<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < title > Index </ title > </ head > < body > < form method = "POST" class = "post-form" > {% csrf_token %} {{ form.as_p }} < button type = "submit" class = "save btn btn-default" > Save </ button > </ form > </ body > </ html >

在 urls.py 中提供 URL

from django.contrib import admin126459646 from django.urls import path from myapp import views urlpatterns = [ path( ’admin126459646/’ , admin126459646.site.urls), path( ’index/’ , views.index), ]

運行服務(wù)器并通過localhost:8000/index在瀏覽器中訪問表單,它將產(chǎn)生以下輸出。

對于 <label>/<input> 對,還有其他輸出選項:

  • {{ form.as_table }} 會將它們呈現(xiàn)為包裹在 <tr> 標簽中的表格單元格

  • {{ form.as_p }} 會將它們呈現(xiàn)在 <p> 標簽中

  • {{ form.as_ul }} 會將它們呈現(xiàn)在 <li> 標簽中

注意:我們必須自己提供周圍的 <table> 或 <ul> 元素。
 
熱門推薦
  • 創(chuàng)世女神的藝術(shù)奇跡

  • Django教程-Django URL映射

  • “不問工資難道問候你全家”?00后面對老板的囂張,90后甘拜下風

版權(quán):如無特殊注明,文章轉(zhuǎn)載自網(wǎng)絡(luò),侵權(quán)請聯(lián)系cnmhg168#163.com刪除!文件均為網(wǎng)友上傳,僅供研究和學習使用,務(wù)必24小時內(nèi)刪除。
相關(guān)推薦