访问烧瓶应用程序的配置给出"模块对象没有属性__getitem __"

时间:2016-02-17 20:28:46

标签: python flask

我正在尝试对我的烧瓶应用实施电子邮件身份验证,用户必须在完全在线注册过程中确认其电子邮件地址。我猜也许是我在app.py中设置配置的方式,或者我应该修改我的config.py?有什么想法吗?

这是我的app.py

....
import config

app = Flask(__name__)

# Import configuration
app.config.from_object(config.DevelopmentConfig)

# Initialize extensions
lm = LoginManager()
lm.session_protection = 'strong'
lm.init_app(app)
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
moment = Moment(app)
mail = Mail(app)
......

这是我的token.py

from itsdangerous import URLSafeTimedSerializer
import app


def generate_confirmation_token(email):
    serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
    return serializer.dumps(email, salt=app.config['SECRET_PASSWORD_SALT'])

def confirm_token(token, expiration=3600):
    serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
    try:
        email = serializer.loads(token, salt=app.config['SECRET_PASSWORD_SALT'], max_age=expiration)
    except:
        return False
    return email

这是由flask调试生成的错误日志

TypeError
TypeError: 'module' object has no attribute '__getitem__'

Traceback (most recent call last)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/vagrant/catalog/app/main/routes.py", line 20, in index
token = generate_confirmation_token(user.email)
File "/vagrant/catalog/app/token.py", line 6, in generate_confirmation_token
serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
TypeError: 'module' object has no attribute '__getitem__'

1 个答案:

答案 0 :(得分:2)

您有一个名为app的包,并导入名为config的模块。 app模块还定义了一个名为app的变量,该变量具有名为config的属性。您导入了错误的app

from app import app
app.config['SECRET_KEY']