组织一个 Flask 项目通常需要遵循一定的结构,以便代码清晰、可维护。下面是一个典型的 Flask 项目结构:
my_flask_app/
│
├── app/
│ ├── __init__.py
│ ├── models.py
│ ├── views.py
│ ├── forms.py
│ ├── templates/
│ │ ├── base.html
│ │ ├── index.html
│ └── static/
│ ├── css/
│ ├── js/
│ └── images/
│
├── migrations/
│
├── tests/
│ ├── __init__.py
│ ├── test_app.py
│
├── venv/
│
├── config.py
│
├── manage.py
│
├── requirements.txt
│
└── README.md
目录和文件说明
app/
__init__.py
: 初始化 Flask 应用,注册蓝图和扩展。models.py
: 数据库模型定义。views.py
: 视图函数,处理路由和请求。forms.py
: 表单定义(如果使用 Flask-WTF)。templates/
: HTML 模板文件。static/
: 静态文件(CSS、JavaScript、图像等)。
migrations/
- 数据库迁移文件夹,用于存储 Alembic 生成的迁移脚本。
tests/
venv/
config.py
manage.py
requirements.txt
README.md
安装:
pip install Flask-WTF
一. 在 forms.py中
# forms.pyfrom flask_wtf.file import (FileRequired, # 文件验证MultipleFileField, # 多文件字段FileAllowed, FileField, FileSize, # 文件字段 文件验证FileStorage
)
from wtforms.fields import simple # 导入字段
from wtforms import validators # 导入验证器
from wtforms import widgets # 导入组件
from wtforms.form import Form
from flask_wtf.form import FlaskForm, Form # 导入form""" 验证码 """
from flask_wtf import Form, RecaptchaField""" URL """
from wtforms.fields import URLField
from wtforms.validators import url""" 文件 """
from flask_wtf import file # 文件
from flask_wtf import recaptcha # 验证码
from flask_wtf.file import (FileRequired, # 文件字段MultipleFileField # 多文件字段)class LoginForm(FlaskForm):username = simple.StringField("用户名", validators=[validators.Length(min=4, max=6, message="长度只能是4~6位"),validators.DataRequired(),validators.Regexp(r'\d+', message="只能是数字")],widget=widgets.TextInput())password = simple.PasswordField("密码", validators=[validators.Length(min=6, message="长度不能少于6位"),validators.DataRequired()],widget=widgets.PasswordInput())confirm = simple.PasswordField('确认密码', validators=[validators.Length(min=6, message="长度不能少于6位"),validators.DataRequired(),validators.EqualTo("password",message="密码不一致")],widget=widgets.PasswordInput())submit = simple.SubmitField("登录")class UserForm(FlaskForm, Form):username = simple.StringField('用户名',[validators.Length(min=4, max=25, message="长度最小不能少于4最大不能大于25"),validators.DataRequired(message="不能为空"),validators.Regexp(r'[A-Za-z0-9_\-\u4e00-\u9fa5]+')], widget=widgets.TextInput(),render_kw={"class": "username"})email = simple.StringField('邮箱', [validators.Length(min=6, max=35)], widget=widgets.EmailInput())password = simple.PasswordField('密码', [validators.Length(min=6, message="长度不能少于6位"),validators.DataRequired(message="不可为空"), ],widget=widgets.PasswordInput())confirm = simple.PasswordField('确认密码', [validators.Length(min=6, message="长度不能少于6位"),validators.DataRequired(message="不可为空"),validators.EqualTo('password', message='密码不一致')],widget=widgets.PasswordInput())accept_tos = simple.BooleanField('接受协议', [validators.DataRequired()], widget=widgets.CheckboxInput())submit = simple.SubmitField("登录", widget=widgets.SubmitInput())class UploadForm(FlaskForm):file = simple.FileField("文件", validators=[file.FileRequired(), file.FileAllowed("jpg, png, gif",message="只允许上传jpg, png, gif的图片格式")])multiple = simple.MultipleFileField("多文件")submit = simple.SubmitField("提交")class LinkForm(Form):url = URLField(validators=[url()])class SignupForm(Form):username = simple.StringField('Username')recaptcha = RecaptchaField()
1. 导包
2. 登录 form
3. 字段验证
4. 用户form
5. 文件form
6. URL form
7. 验证码form
# 待完成
二. 后端部分
# 导入 form# 路由 渲染到前端
三. 前端部分
<!-- 遍历form -->