0
我在應用程序中集成了Flask和SQLAlchemy,但是可以理解,爲什麼在登錄頁面中獲取大堆棧跟蹤。現在我將在從Debian切換到Mac OS後執行這些錯誤。所有在virtuaelnv中使用過的庫。'引擎'對象沒有屬性'drivername'
我現在所擁有的這些東西:
在flask_app.py 1):
from flask import Flask, request, session
from flask_sqlalchemy import SQLAlchemy
from db import tables as dbTables
from db.queries import engine as db_engine
app = Flask(__name__, template_folder='./web/templates/', static_folder='./web/static/', static_url_path='')
app.config['SECRET_KEY'] = 'some_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = db_engine
db_connection = SQLAlchemy(app)
import web.admin
import web.views
if __name__ == "__main__":
from web.admin import *
from web.views import *
app.run(port=8500, debug=True)
2)它我queries.py,其中包含該字符串和代碼採取和PostgreSQL數據庫的一些數據:
import datetime
from hashlib import sha256
from time import gmtime, strftime
import sqlalchemy
import sqlalchemy.exc
from sqlalchemy import and_, func, asc
from sqlalchemy.orm import sessionmaker
from tables import File as FileTable
from tables import Users, FileServer, FileSpace, Catalog
engine = sqlalchemy.create_engine("postgresql://dev:[email protected]/csan", pool_size=20, max_overflow=0)
Session = sessionmaker(bind=engine)
# and some queries for DB
# ...
3)而這個堆棧跟蹤,發送給http://localhost:8500/admin/login
POST請求後:
AttributeError
AttributeError: 'Engine' object has no attribute 'drivername'
Traceback (most recent call last)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_admin/base.py", line 62, in inner
return f(self, *args, **kwargs)
File "/Users/savicvalera/code/Helenae/Helenae/helenae/web/admin.py", line 42, in login_view
if helpers.validate_form_on_submit(form):
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_admin/helpers.py", line 47, in validate_form_on_submit
return is_form_submitted() and form.validate()
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/wtforms/form.py", line 310, in validate
return super(Form, self).validate(extra)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/wtforms/form.py", line 152, in validate
if not field.validate(self, extra):
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/wtforms/fields/core.py", line 200, in validate
stop_validation = self._run_validation_chain(form, chain)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/wtforms/fields/core.py", line 220, in _run_validation_chain
validator(form, self)
File "/Users/savicvalera/code/Helenae/Helenae/helenae/web/forms.py", line 19, in validate_login
user = get_user(self.login.data)
File "/Users/savicvalera/code/Helenae/Helenae/helenae/web/forms.py", line 11, in get_user
return db_connection.session.query(dbTables.Users).filter_by(name=user).first()
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 150, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 910, in __call__
return self.registry.setdefault(key, self.createfunc())
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 139, in __init__
bind=db.engine,
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 780, in engine
return self.get_engine(self.get_app())
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 797, in get_engine
return connector.get_engine()
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 470, in get_engine
self._sa.apply_driver_hacks(self._app, info, options)
File "/Users/savicvalera/code/Helenae/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 739, in apply_driver_hacks
if info.drivername.startswith('mysql'):
AttributeError: 'Engine' object has no attribute 'drivername'
爲什麼不使用[Flask-SQLAlchemy](https://pythonhosted.org/Flask-SQLAlchemy/)來處理與Flask的SQLAlchemy集成? – 2014-12-06 19:00:56
@MartijnPieters Flask-SQLAlchemy已經在項目中使用了(看看flask_app.py) – Relrin 2014-12-06 19:03:42
那你爲什麼還要用'from sqlalchemy.orm import sessionmaker'? – 2014-12-06 19:06:13