gpt4 book ai didi

python-3.x - 属性错误 : 'NoneType' object has no attribute 'drivername'

转载 作者:行者123 更新时间:2023-12-04 15:06:31 26 4
gpt4 key购买 nike

当我启动我的应用程序时出现此错误。显然它与 SQLAlchemy 有一些关系。我一直在 Corey Schaffer Flask 教程的帮助下处理这个例子。

> File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Matea\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Matea\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Matea\myblog\myblog\main\routes.py", line 11, in home
posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 514, in __get__
return type.query_class(mapper, session=self.sa.session())
File "C:\Users\Matea\venv\lib\site-packages\sqlalchemy\orm\scoping.py", line 74, in __call__
return self.registry()
File "C:\Users\Matea\venv\lib\site-packages\sqlalchemy\util\_collections.py", line 1001, in __call__
return self.registry.setdefault(key, self.createfunc())
File "C:\Users\Matea\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2950, in __call__
return self.class_(**local_kw)
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 143, in __init__
bind = options.pop('bind', None) or db.engine
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 877, in engine
return self.get_engine()
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 896, in get_engine
return connector.get_engine()
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 556, in get_engine
self._sa.apply_driver_hacks(self._app, info, options)
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 830, in apply_driver_hacks
if info.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'

这是一些代码:
这是我的 初始化 .py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from myblog.config import Config


db = SQLAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'users.login'
login_manager.login_message_category = 'info'
mail = Mail()


def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)

db.init_app(app)
bcrypt.init_app(app)
login_manager.init_app(app)
mail.init_app(app)

from myblog.users.routes import users
from myblog.posts.routes import posts
from myblog.main.routes import main
from myblog.errors.handlers import errors
app.register_blueprint(users)
app.register_blueprint(posts)
app.register_blueprint(main)
app.register_blueprint(errors)

return app

这段代码来自 config.py
import os


class Config:
SECRET_KEY = os.environ.get('SECRET_KEY')
SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URL')
MAIL_SERVER = 'smtp.googlemail.com'
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = os.environ.get('EMAIL_USER')
MAIL_PASSWORD = os.environ.get('EMAIL_PASS')

我希望你能帮助我,如果有什么遗漏,请告诉我,谢谢!

最佳答案

如果您在 中正确设置了环境变量.bash_proflie

    export EMAIL_USER='youremail@gmail.com'
export EMAIL_PASS='Yourpassword'
export SQLALCHEMY_DATABASE_URI='sqlite:///site.db'
export SECRET_KEY='secretkeyxxxxxxxxxxxxxxxx'

然后尝试重新启动计算机。
如果它仍然失败
*尝试在python中创建数据库*
from your yourflaskapp import db
db.create_all()

如果您的应用程序在蓝图中
    use from yourapp import create_app
app = create_app()
app.app_context().push()

Always refresh your text editor and the terminal after making changes. Also try to make sure you are running your app in the right directory.



有关蓝图使用的更多信息,请访问

http://flask-sqlalchemy.pocoo.org/2.3/contexts/

关于python-3.x - 属性错误 : 'NoneType' object has no attribute 'drivername' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51727943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com