无法在Flask中设置config.py

时间:2019-12-14 01:16:42

标签: python flask

我正在尝试设置config.py以使用CLIENT_IDCLIENT_SECRET。当这些环境在def xxxx()中变量时,这不是错误。然后将其用作current_app.config('XXXX'),但不能在def xxxx()之外调用。

错误:

  

这通常意味着您尝试使用所需的功能   以某种方式与当前应用程序对象交互。解决   为此,使用app.app_context()设置应用程序上下文。见   文档以获取更多信息。

auth.py

import functools
import pdb
from flask import (
    Blueprint, flash, g, redirect, render_template, request, url_for, current_app
)
import slack
from app.db import (
    get_or_create_team, get_or_create_user, get_or_create_team_user
)
from flask_login import login_user, logout_user, current_user, LoginManager

bp = Blueprint('auth', __name__, url_prefix='/auth')
client_id = current_app.config['CLIENT_ID']
client_secret = current_app.config['CLIENT_SECRET']

config.py

# client
CLIENT_ID='XXXXXXXXXXXXXXXX'
CLIENT_SECRET='XXXXXXXXXXXXXXXX'

1 个答案:

答案 0 :(得分:0)

来自:

from flask import (
    Blueprint, flash, g, redirect, render_template, request, url_for, current_app
)

client_id = current_app.config['CLIENT_ID']
client_secret = current_app.config['CLIENT_SECRET']

至:

from os import environ

client_id = environ.get('CLIENT_ID')
client_secret = environ.get('CLIENT_SECRET')