(1)初始化
from flaskimport Flask,render_template,flash
app = Flask(__name__)
flask的构造函数有一个指定的参数,及应用主模块或包的名称,可以用python的__name__变量代替这个
(2)路由函数
@app.route('/')
def index():
return render_template('index.html',form=form,name=name)
(3)服务器
在pycharm当中运行的时候,默认的情况他不会自动刷新页面,需要设置port 和 debug 设置为True 才可以实现自动刷新
(4)jinja2 模板
(5)bootstrap-flask(现在使用的是这个,以前的flask-bopotstrap已经不再维护了,所以现在使用的是这个库)
1:初始化,Bootstrap(app)
详细的信息访问相关的官网 https://bootstrap-flask.readthedocs.io/en/latest/index.html
(6)自定义错误页面(主要是404,500页面)
@app.errorhandler(404)
@app.errorhandler(500)
用这两个装饰器就可以了
![](https://img.haomeiwen.com/i15545292/ac3ddc1181a00f83.png)
(7)链接(主要区别的是硬链接)
有两个函数 url_for() --- 可以传入端点(视图函数的名称),_external=True/False(是否生成绝对路径),也可以传入静态参数和动态参数
(8)静态文件的导入
利用url_for('static' , filename='css/123.css,_external) -- 可以像这样就行了
(9)表单类
主要是运用了flask-wtf 扩展库来实现
官方网址 http://www.pythondoc.com/flask-wtf/
(10)闪现消息(flash)
1:在视图中定义 flash 消息
2:在模板中渲染 flash(通过get_flashed_messages() 函数可以渲染)
![](https://img.haomeiwen.com/i15545292/65532c98cc36ef44.png)
![](https://img.haomeiwen.com/i15545292/eb4cb9553c19bdb1.png)
网友评论