# flask2 **Repository Path**: hcfox/flask2 ## Basic Information - **Project Name**: flask2 - **Description**: No description available - **Primary Language**: Python - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2016-06-03 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #flask2 ## 安装 1. ```sudo apt-get install python-virtualenv``` ``` virtaulenv venv ``` 2. ``` source ~/.venv/python3.5/bin/activiate ``` 激活py3.5环境 3. ``` pip install flask ``` 安装flask 4. jinja2 模板 在controller里面通过 ``` render_template("模板文件",key=value) ``` 渲染模板 模板默认在templates目录中 5. Bootstrap ``` pip install flask-bootstrap ``` 引用 ``` from flask.ext.bootstrap import Bootstrap ``` 引用 ``` app = Flask(__name__) bootstrap = Bootstrap(app) ``` 页面中通过模板继承的方式使用 ``` {% extends "bootstrap/base.html" %} {% block content %}
本地时间 {{ moment(current_time).format('LLL') }}
距离 {{ moment(current_time).fromNow(refresh=True) }}
``` 7. 表单 ``` pip install flask-wtf ``` 配置 ``` app.config['SECRET_KEY'] = 'hard to guess string' ``` 表单类 ``` class NameForm(Form): name = StringField('你的姓名',validators = [ Required()]) submit = SubmitField('提交') ``` 使用 ``` @app.route('/',methods=['GET','POST']) def index(): name = None form = NameForm() if form.validate_on_submit(): old_name = session.get('name') if old_name is not None and old_name != form.name.data: ``` 8. 重定向 ``` redirect(url_for('index')) ``` 9. Flash消息 ``` from flask import Flask, flash ``` controller类中使用 ``` flash('Looks like you have changed your name!') ``` 页面中显示 ``` {% for message in get_flashed_messages()%}