我正在尝试使用工厂设计模式来实现flask应用程序。由于此错误而无法运行应用
127.0.0.1 - - [24/Jun/2018 18:07:14] "OPTIONS /api/v1/users/register HTTP/1.1" 200 -
token name 'app' is not defined
127.0.0.1 - - [24/Jun/2018 18:07:15] "POST /api/v1/users/register HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb)
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask/_compat.py", line 35, in
reraise raise value
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/jacobo/.local/share/virtualenvs/alen-yVjuFfoa/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/mnt/c/Users/Jacobo/Projects/alen/server/users/api.py", line 25, in register
'token': token.decode()
AttributeError: 'NameError' object has no attribute 'decode'
我的项目结构如下:
/ myapp
/users
api.py
models.py
app.py
database.py
extensions.py
settings.py
run.py
我认为问题是该应用无法识别应用配置。但是我不知道如何使它起作用。
app.py
from flask import Flask, render_template
from server import users
from server.extensions import bcrypt, db, migrate, cors
from server.settings import DevConfig
def create_app(config_object=DevConfig):
app = Flask(__name__,
static_folder="../frontend/public",
template_folder="../frontend/public")
app.config.from_object(config_object)
register_extensions(app)
register_blueprints(app)
return app
def register_extensions(app):
bcrypt.init_app(app)
db.init_app(app)
migrate.init_app(app, db)
cors.init_app(app)
return None
def register_blueprints(app):
app.register_blueprint(users.api.blueprint)
return None
settings.py
import os
class Config(object):
SECRET_KEY = 'p9Bv<3Eid9%$i01'
APP_DIR = os.path.abspath(os.path.dirname(__file__)) # This directory
PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir))
BCRYPT_LOG_ROUNDS = 13
DEBUG_TB_ENABLED = False
DEBUG_TB_INTERCEPT_REDIRECTS = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_DATABASE_URI = database
class ProdConfig(Config):
ENV = 'prod'
DEBUG = False
DEBUG_TB_ENABLED = False
class DevConfig(Config):
ENV = 'dev'
DEBUG = True
DEBUG_TB_ENABLED = True
/users/api.py
from flask import Blueprint, jsonify, request, make_response
from .models import User
from server.extensions import db
from sqlalchemy.exc import IntegrityError
blueprint = Blueprint('user', __name__, url_prefix='/api/v1/users')
@blueprint.route("/register", methods=['POST'])
def register():
data = request.get_json()
user = User.query.filter_by(email=data.get('email')).first()
if not user:
user = User(
email=data["email"],
password=data["password"]
)
db.session.add(user)
db.session.commit()
token = user.encode_auth_token(user.id)
print('token', token)
responseObject = {
'status': 'success',
'message': 'Succesfully registered.',
'token': token.decode()
}
return make_response(jsonify(responseObject)), 201
else:
responseObject = {
'status': 'fail',
'message': 'User already exists. Please log in.'
}
return make_response(jsonify(responseObject)), 202
/users/models.py
import datetime as dt
import jwt
from server.database import Column, Model, SurrogatePK
from server.database import db, reference_col, relationship
from server.extensions import bcrypt
class Role(SurrogatePK, Model):
__tablename__ = 'roles'
name = Column(db.String(80), unique=True, nullable=False)
user_id = reference_col('users', nullable=True)
user = relationship('User', backref='roles')
def __init__(self, name, **kwargs):
db.Model.__init__(self, name=name, **kwargs)
def __repr__(self):
"""Represent instance as a unique string."""
return '<Role({name})>'.format(name=self.name)
class User(SurrogatePK, Model):
__tablename__ = 'users'
email = Column(db.String(80), unique=True, nullable=False)
password = Column(db.String(255), nullable=True)
created_at = Column(db.DateTime, nullable=False,
default=dt.datetime.utcnow)
active = Column(db.Boolean(), default=False)
is_admin = Column(db.Boolean(), default=False)
def __init__(self, email, password=None, **kwargs):
db.Model.__init__(self, email=email, **kwargs)
if password:
self.set_password(password)
else:
self.password = None
def set_password(self, password):
self.password = bcrypt.generate_password_hash(password).decode()
def check_password(self, value):
return bcrypt.check_password_hash(self.password, value)
def __repr__(self):
return '<User({email!r})>'.format(email=self.email)
def encode_auth_token(self, user_id):
try:
payload = {
'exp': dt.datetime.utcnow() + dt.timedelta(days=0, seconds=5),
'iat': dt.datetime.utcnow(),
'sub': user_id
}
return jwt.encode(
payload,
app.config.get('SECRET_KEY'),
algorithm='HS256'
)
except Exception as e:
return e
@staticmethod
def decode_auth_token(auth_token):
"""
Validates the auth token
:param auth_token:
:return: integer|string
"""
try:
payload = jwt.decode(auth_token, app.config.get('SECRET_KEY'))
is_blacklisted_token = BlacklistToken.check_blacklist(auth_token)
if is_blacklisted_token:
return 'Token blacklisted. Please log in again.'
else:
return payload['sub']
except jwt.ExpiredSignatureError:
return 'Signature expired. Please log in again.'
except jwt.InvalidTokenError:
return 'Invalid token. Please log in again.'
预先感谢
答案 0 :(得分:2)
问题很明显,与Flask本身无关。
File "/mnt/c/Users/Jacobo/Projects/alen/server/users/api.py", line 25, in register
'token': token.decode()
AttributeError: 'NameError' object has no attribute 'decode'
对象token
不是具有decode
的东西(我假设它应该是bytes
);而是一个NameError
对象。
如果我们追溯到token
的来源,则它来自您的def encode_auth_token(self, user_id):
函数–问题是所有返回异常都没有得到适当处理(或者更确切地说,如果没有处理,那么他们会引发异常):
except Exception as e:
return e
我建议完全删除该异常处理,以便您找到根本原因(例如SECRET_KEY
必须是字节,而不是字符串,但是您会发现的)。