TML:完成頁面的內容展示
CSS:完成頁面樣式的控制,美化頁面,完成頁面的布局。
表單:用于采集用戶輸入的數據。用于和服務器進行交互。
form:用于定義表單的。可以定義一個范圍(代表用戶采集數據的范圍)
屬性:action:指定提交數據的url(指的就是把數據提交到哪里)
method:指定提交方式
分類:一共有7種,2種比較常用。
get:1.請求參數會在地址欄顯示
2.請求參數的長度是有限制的。
3.請求不安全
post:1.請求參數不會在地址欄顯示,會封裝在請求體中。
2.請求參數的長度沒有限制
3.較為安全
表單里面的數據要想被提交,必須指定它的name屬性
表單項標簽
input:可以通過type屬性值,改變元素展示的樣式。
type屬性:text:文本輸入框,默認值
placeholder:指定輸入框的提示信息,當輸入框的內容發生變化,會自動情況提示信息。
password:密碼輸入框
radio:1.單選框(要想讓多個單選框實現單選的效果,則多個單選框的name屬性值必須一樣)
2.一般會給每一個單選框提供value屬性,指定其被選中后提交的值。
3.checked屬性可以指定默認值
checkbox:復選框:
1.一般會給每一個單選框提供value屬性,指定其被選中后提交的值。
2.checked屬性可以指定默認值
file:文件選擇框
hidden:隱藏域,用于提交一些信息
按鈕:
submit:提交按鈕??梢蕴峤槐韱?/p>
button:普通按鈕
image:圖片提交按鈕
src屬性指定圖片的路徑
label:指定輸入項的文字描述信息
注意:lable的for屬性一般會和input的id屬性值對應。如果對應了,點擊lable區域,會讓input輸入框獲取焦點。
select:下拉列表
子元素:option,指定列表項
textarea:文本域
eform 是一個用于創建 Web 表單的 Python 庫,提供了許多用于處理表單數據及其驗證和呈現的功能。 deform 幫助程序員創建簡單或復雜的 Web 表單,并使其易于與 Pyramid、Django 和 Flask 等 Python Web 框架集成。
此外,deform 還提供了許多支持組件,如日期選擇器、下拉列表、多選框、單選按鈕等,這些組件可以幫助開發人員快速創建各種類型的 Web 表單。
使用 pip 進行安裝:
pip install deform
"""Self-contained Deform demo example."""
from __future__ import print_function
from pyramid.config import Configurator
from pyramid.session import UnencryptedCookieSessionFactoryConfig
from pyramid.httpexceptions import HTTPFound
import colander
import deform
class ExampleSchema(deform.schema.CSRFSchema):
name = colander.SchemaNode(
colander.String(),
title="Name")
age = colander.SchemaNode(
colander.Int(),
default=18,
title="Age",
description="Your age in years")
def mini_example(request):
"""Sample Deform form with validation."""
schema = ExampleSchema().bind(request=request)
# Create a styled button with some extra Bootstrap 3 CSS classes
process_btn = deform.form.Button(name='process', title="Process")
form = deform.form.Form(schema, buttons=(process_btn,))
# User submitted this form
if request.method == "POST":
if 'process' in request.POST:
try:
appstruct = form.validate(request.POST.items())
# Save form data from appstruct
print("Your name:", appstruct["name"])
print("Your age:", appstruct["age"])
# Thank user and take him/her to the next page
request.session.flash('Thank you for the submission.')
# Redirect to the page shows after succesful form submission
return HTTPFound("/")
except deform.exception.ValidationFailure as e:
# Render a form version where errors are visible next to the fields,
# and the submitted values are posted back
rendered_form = e.render()
else:
# Render a form with initial default values
rendered_form = form.render()
return {
# This is just rendered HTML in a string
# and can be embedded in any template language
"rendered_form": rendered_form,
}
def main(global_config, **settings):
"""pserve entry point"""
session_factory = UnencryptedCookieSessionFactoryConfig('seekrit!')
config = Configurator(settings=settings, session_factory=session_factory)
config.include('pyramid_chameleon')
deform.renderer.configure_zpt_renderer()
config.add_static_view('static_deform', 'deform:static')
config.add_route('mini_example', path='/')
config.add_view(mini_example, route_name="mini_example", renderer="templates/mini.pt")
return config.make_wsgi_app()
總體而言,如果你需要在 Python Web 應用程序中處理表單,Deform 是一個非常好用的庫。它提供了許多有用的功能,并且易于使用和集成到現有的 Web 框架中。
演示地址:https://deformdemo.pylonsproject.org/
Github 地址:https://github.com/Pylons/deform
一篇文章我們說了單行文本框和多行文本框,今天呢我們繼續看一下表單的其它控件:單選框、復選框、下拉框。
在我們表單頁面中,經常會有選擇性別或者選擇愛好這類的內容,使用選擇框是一個好主意,html中有兩種選擇框,即單選框和復選框,兩者的區別是單選框中的選項用戶只能選擇一項,而復選框中用戶可以任意選擇多項,甚至全選。
使用語法:
單選框:<input type="radio" value="值" name="名稱" checked="checked"/>
復選框:<input type="checkbox" value="值" name="名稱" checked="checked"/>
詳細講解:
1、type: 當 type="radio" 時,控件為單選框;當 type="checkbox" 時,控件為復選框
2、value:提交數據到服務器的值(后臺程序使用)
3、name:為控件命名,這里要注意同一組的單選按鈕,name 取值一定要一致(具體可見下邊的參考練習)。
4、checked:當設置 checked="checked"(也可以直接簡寫成checked) 時,該選項被默認選中
使用練習:
我們創建一個表單,表單里邊包含姓別(男、女)選擇的單選框,默認選中男以及愛好(唱歌、打游戲、繪畫、旅游)選擇的多選框,默認選中唱歌。具體的代碼如下圖所示:
在網頁中的顯示效果就如下圖所示:
下拉框也是我們常用的一個表單控件,多用于選擇城市地區等。
使用語法:
<select>
<option value="向服務器提交的內容" selected="selected">網頁顯示的內容</option>
</select>
詳細講解:
1、option:option為select下拉子元素,可以有一個或多個,寫法類似ul和li,其中的value內容為提交數據到服務器的值(后臺程序使用)
2、selected:當設置 selected="selected"(也可以直接簡寫成selected) 時,該選項被默認選中
使用練習:
我們創建一個表單,表單里邊包含一個城市的下拉框,下拉框中有北京、上海、天津這三個城市,其中默認選中天津。具體的代碼如下圖所示:
在網頁中的顯示效果就如下圖所示:
好了,本篇文章就先給大家介紹這幾個表單控件的語法以及使用,下篇文章我們將介紹按鈕的語法及使用以及完整的表單練習演示,記得平時要多加練習才是王道。
每日金句:做人要像竹子一樣每前進一步,都要做一次小結。喜歡我的文章的小伙伴記得關注一下哦,每天將為你更新最新知識。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。