Python flask_wtf 模块,CSRFProtect() 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用flask_wtf.CSRFProtect()

项目:Flask-MVC-Template    作者:CharlyJazz    | 项目源码 | 文件源码
def create_app(config_name):
    global user_datastore
    app = Flask(__name__)

    app.config.from_object(app_config[config_name])

    csrf = CSRFProtect()
    csrf.init_app(app)

    assets = Environment(app)
    create_assets(assets)

    via = Via()
    via.init_app(app)

    # Code for desmostration the flask upload in several models - - - -

    from user import user_photo
    from restaurant import restaurant_photo
    from food import food_photo

    configure_uploads(app, (restaurant_photo, food_photo, user_photo))

    engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
    if not database_exists(engine.url):
        create_database(engine.url)

    security = Security(app, user_datastore, register_form=SecurityRegisterForm)

    create_security_admin(app=app, path=os.path.join(os.path.dirname(__file__)))

    with app.app_context():
        db.init_app(app)
        db.create_all()
        user_datastore.find_or_create_role(name='admin', description='Administrator')
        db.session.commit()
        user_datastore.find_or_create_role(name='end-user', description='End user')
        db.session.commit()

    @app.route('/', methods=['GET'])
    @app.route('/home', methods=['GET'])
    def index():
        return render_template('index.html')

    @app.errorhandler(403)
    def forbidden(error):
        return render_template('error/403.html', title='Forbidden'), 403

    @app.errorhandler(404)
    def page_not_found(error):
        return render_template('error/404.html', title='Page Not Found'), 404

    @app.errorhandler(500)
    def internal_server_error(error):
        db.session.rollback()
        return render_template('error/500.html', title='Server Error'), 500

    return app