Flask - blueprint 사용법

김예지·2021년 6월 8일
0

플라스크로 공동작업을 할 때는 Blueprint라는 라이브러리를 이용해 github에서 서로 충돌나지 않고 파일을 나눠서 작업할 수 있도록 한다.

본 예시에서는 app.py에 auth.py를 블루프린트로 추가한다.

설정 방법
1. app.py 설정

import auth
app = Flask(__name__)
@app.route('/')
def home():
    token_receive = request.cookies.get('mytoken')
    try:
        payload = jwt.decode(token_receive, SECRET_KEY, algorithms=['HS256'])

        return render_template('index.html')
    except jwt.ExpiredSignatureError:
        return redirect(url_for("auth.login", msg="로그인 시간이 만료되었습니다."))
    except jwt.exceptions.DecodeError:
        return redirect(url_for("auth.login", msg="로그인 정보가 존재하지 않습니다."))


app.register_blueprint(auth.bp)

app.run('0.0.0.0', port=5000, debug=True)
  1. db.py 설정
def get_db():
    client = MongoClient('IP', 27017, username="id", password="pw")

    return client.db_name
  1. 작업할 파일 설정
import db

SECRET_KEY = 'secret'
db = db.get_db()

bp = Blueprint("auth", __name__)


@bp.route('/login')
def login():
    msg = request.args.get("msg")
    return render_template('login.html', msg=msg)
profile
새싹

0개의 댓글