Django教程-表單
Django教程-表單
gh_1d7504e4dee1
回復:python,領(lǐng)取Python面試題。分享Python教程,Python架構(gòu)師教程,Python爬蟲,Python編程視頻,Python腳本,Pycharm教程,Python微服務(wù)架構(gòu),Python分布式架構(gòu),Pycharm注冊碼。
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è)置輸入值的長度。
Python從入門到進階知識手冊
<
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
)
在 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
>
from
django.contrib
import
admin126459646
from
django.urls
import
path
from
myapp
import
views
urlpatterns = [
path(
’admin126459646/’
, admin126459646.site.urls),
path(
’index/’
, views.index),
]
對于 <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后甘拜下風
-
2023年血糖新標準公布,不是3.9-6.1,快來看看你的血糖正常嗎? 2023-02-07
-
2023年各省最新電價一覽!8省中午執(zhí)行谷段電價! 2023-01-03
-
PPT導出高分辨率圖片的四種方法 2022-09-22
-
2023年最新!國家電網(wǎng)27家省級電力公司負責人大盤點 2023-03-14
-
全國消防救援總隊主官及簡歷(2023.2) 2023-02-10
-
盤點 l 中國石油大慶油田現(xiàn)任領(lǐng)導班子 2023-02-28
-
我們的前輩!歷屆全國工程勘察設(shè)計大師完整名單! 2022-11-18
-
關(guān)于某送變電公司“4·22”人身死亡事故的快報 2022-04-26
