码迷,mamicode.com
首页 > 其他好文 > 详细

flask项目搭建

时间:2020-07-29 10:28:10      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:name   url   object   图片   manage   require   cat   alc   debug   

config---配置

技术图片
 1 # -*- coding = utf-8 -*-
 2 # @Time : 2020/7/28 17:51
 3 # @Author : 贾伟文
 4 # @File : config.py
 5 # @Software : PyCharm
 6 
 7 DB_URI = mysql://root:123456@localhost:3306/icbc?charset=utf8
 8 SQLALCHEMY_DATABASE_URI = DB_URI
 9 SQLALCHEMY_TRACK_MODIFICATIONS = False
10 SECRET_KEY = abfhdj
View Code

exts

技术图片
# -*- coding = utf-8 -*-
# @Time : 2020/7/28 17:48
# @Author : 贾伟文
# @File : exts.py
# @Software : PyCharm

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
View Code

manage

技术图片
# -*- coding = utf-8 -*-
# @Time : 2020/7/28 18:09
# @Author : 贾伟文
# @File : manage.py
# @Software : PyCharm

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import app
from exts import db

manager = Manager(app)
Migrate(app, db)
manager.add_command(db, MigrateCommand)


if __name__ == __main__:
    manager.run()
View Code

app

技术图片
 1 from flask import Flask, render_template, session
 2 from front import front_bp, login_required
 3 import config
 4 from exts import db
 5 
 6 app = Flask(__name__)
 7 app.config.from_object(config)
 8 app.register_blueprint(front_bp)
 9 db.init_app(app)
10 
11 
12 @app.route(/, endpoint=index)
13 @login_required
14 def hello_world():
15     user_id = session.get(user_id)
16     return render_template(front/index.html, user_id=user_id)
17 
18 
19 if __name__ == __main__:
20     app.run(debug=True)
View Code

login_requierd---登录验证

# -*- coding = utf-8 -*-
# @Time : 2020/7/28 21:38
# @Author : 贾伟文
# @File : login_required.py
# @Software : PyCharm
from flask import session, redirect, url_for


def login_required(func):
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id")
        if not user_id:
            return redirect(url_for(front.login))
        return func(*args, **kwargs)
    return wrapper

 

flask项目搭建

标签:name   url   object   图片   manage   require   cat   alc   debug   

原文地址:https://www.cnblogs.com/YiwenGG/p/13395015.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!