一、原生表單
form.html
{% extends 'common/base.html' %}{% block title %} 原生表單{% endblock %}{% block pagecontent %}{# <form action="{{ url_for('check') }}" method="post">#} <form action="{{ url_for('form') }}" method="post"> <p>用戶名: <input type="text" name="username" placeholder="請輸入用戶名" maxlength="12"></p> <p>密碼: <input type="password" name="userpass" placeholder="請輸入密碼..."></p> <p><input type="submit" value="提交"></p> </form>{% endblock %}manage.py
@app.route('/form/')def form(): return render_template('form1.html')#接收表單的數據@app.route('/check/',methods=['POST'])def check(): print(request.form) return '提交過來了'將倆個路由地址合并為同一個
@app.route('/form/',methods=['GET','POST'])def form(): if request.method == 'POST': print(request.form) return render_template('form1.html')二、使用flask-wtf表單擴展庫
作用: 是一個用于表單處理的擴展庫 提供表單的校驗 csrf的功能
pip install flask-wtf
使用
(1) 字段類型
| 字段名稱 | 字段類型 |
|---|---|
| StringField | 普通文本字段 |
| PasswordField | 密碼框 |
| SubmitField | 提交按鈕 |
| TextAreaField | 多行文本域 |
| HiddenField | 隱藏域 |
| DateField | 日期 |
| DateTimeField | 日期時間 |
| IntegerField | 整形 |
| FloatFIeld | 浮點型 |
| RadioField | 單選字段 |
| SelectField | 下拉 |
| FileField | 文件上傳字段 |
| BooleanField | 布爾字段 |
(2) 驗證器
| 驗證器 | 說明 |
|---|---|
| DataRequired | 必填 |
| Length | 長度 min max |
| IPAddress | IP地址 |
| 郵箱 | |
| URL | 地址 |
| Regexp | 正則匹配 |
| EqualTo | 驗證倆個字段值的正確性 |
| NumberRange | 輸入值的范圍 min max |
新聞熱點
疑難解答