Flask-Admin 和 App Engine 中的 admin.add_view(ModelView(User, db.session))
admin.add_view(ModelView(User, db.session)) in Flask-Admin and App Engine
我尝试将使用 Flask-Admin 的 Flask 应用程序部署到 Google App 引擎,但一旦部署它就会返回以下错误:
AttributeError: 'User' object has no attribute 'add_view'
以下行导致错误:
admin.add_view(ModelView(User, db.session))
- 在本地,应用程序运行良好
- 在本地,flasksqlalchemy 连接到 sqllite 数据库,但在 App Engine 上它意味着连接到云 SQL
注意:密码和数据库详细信息是虚构的!
main.py
from flask_login import LoginManager,UserMixin,login_user,login_required,current_user,logout_user
from flask import Flask,request,render_template,redirect,url_for, Markup, send_file, session
from flask_sqlalchemy import SQLAlchemy
import os
from config import config
from flask import Blueprint, render_template, redirect, url_for, request,flash
from werkzeug.security import generate_password_hash, check_password_hash
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
app = Flask(__name__)
config_name='production'
app.config.from_object(config[config_name])
app.config['SECRET_KEY'] = 'randomstring'
app.config['CSRF_ENABLED'] = True
app.config['USE_SESSION_FOR_NEXT'] = True
db = SQLAlchemy(app)
login_manager = LoginManager(app)
login_manager.login_view='login_get'
admin = Admin(app, template_mode='bootstrap3')
class User(UserMixin,db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(30),unique=True)
password = db.Column(db.String(30),unique=True)
if not os.path.isfile('dev_db.db'):
db.create_all()
admin = User(username='Admin', password='somepassword')
person = User(username='Techteam', password='somepassword2')
db.session.add(admin)
db.session.add(person)
db.session.commit()
def __repr__(self):
return '<User %r>' % (self.username)
admin.add_view(ModelView(User, db.session))
@app.route('/')
def index():
return render_template('index.html')
...
这是我的数据库配置:
config.py:
USER='root'
PASSWORD='randompassword'
DATABASE='master'
CONNECTION_NAME='my-app-277020:us-central1:tool'
class Config:
USER_APP_NAME='Tool'
CSRF_ENABLED=True
SECRET_KEY = '2365frtjislkmnqwers'
class Dev_Config(Config):
ENV='development'
DEBUG=True
SQLALCHEMY_DATABASE_URI='sqlite:///dev_db.db'
class Prod_Config(Config):
DEBUG=False
ENV='production'
SQLALCHEMY_DATABASE_URI = (
'mysql+pymysql://{user}:{password}@localhost/{database}'
'?unix_socket=/cloudsql/{connection_name}').format(
user=USER, password=PASSWORD,
database=DATABASE, connection_name=CONNECTION_NAME)
config={'development': Dev_Config,
'production': Prod_Config
错误:
Traceback (most recent call last):
File "/env/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
File "/env/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 92, in init_process
super().init_process()
File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 119, in init_process
self.load_wsgi()
File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
self.wsgi = self.app.wsgi()
File "/env/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
return self.load_wsgiapp()
File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
return util.import_app(self.app_uri)
File "/env/lib/python3.7/site-packages/gunicorn/util.py", line 358, in import_app
mod = importlib.import_module(module)
File "/opt/python3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/srv/main.py", line 39, in <module>
admin.add_view(ModelView(User, db.session))
AttributeError: 'User' object has no attribute 'add_view'
知道为什么会这样吗?
您正在将 admin
的值从 class Admin
(admin = Admin(...)
) 覆盖到 User
(admin = User(...)
)。
最好不要在代码中为不同的目的使用相同的变量名。
我尝试将使用 Flask-Admin 的 Flask 应用程序部署到 Google App 引擎,但一旦部署它就会返回以下错误:
AttributeError: 'User' object has no attribute 'add_view'
以下行导致错误:
admin.add_view(ModelView(User, db.session))
- 在本地,应用程序运行良好
- 在本地,flasksqlalchemy 连接到 sqllite 数据库,但在 App Engine 上它意味着连接到云 SQL
注意:密码和数据库详细信息是虚构的!
main.py
from flask_login import LoginManager,UserMixin,login_user,login_required,current_user,logout_user
from flask import Flask,request,render_template,redirect,url_for, Markup, send_file, session
from flask_sqlalchemy import SQLAlchemy
import os
from config import config
from flask import Blueprint, render_template, redirect, url_for, request,flash
from werkzeug.security import generate_password_hash, check_password_hash
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
app = Flask(__name__)
config_name='production'
app.config.from_object(config[config_name])
app.config['SECRET_KEY'] = 'randomstring'
app.config['CSRF_ENABLED'] = True
app.config['USE_SESSION_FOR_NEXT'] = True
db = SQLAlchemy(app)
login_manager = LoginManager(app)
login_manager.login_view='login_get'
admin = Admin(app, template_mode='bootstrap3')
class User(UserMixin,db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(30),unique=True)
password = db.Column(db.String(30),unique=True)
if not os.path.isfile('dev_db.db'):
db.create_all()
admin = User(username='Admin', password='somepassword')
person = User(username='Techteam', password='somepassword2')
db.session.add(admin)
db.session.add(person)
db.session.commit()
def __repr__(self):
return '<User %r>' % (self.username)
admin.add_view(ModelView(User, db.session))
@app.route('/')
def index():
return render_template('index.html')
...
这是我的数据库配置:
config.py:
USER='root'
PASSWORD='randompassword'
DATABASE='master'
CONNECTION_NAME='my-app-277020:us-central1:tool'
class Config:
USER_APP_NAME='Tool'
CSRF_ENABLED=True
SECRET_KEY = '2365frtjislkmnqwers'
class Dev_Config(Config):
ENV='development'
DEBUG=True
SQLALCHEMY_DATABASE_URI='sqlite:///dev_db.db'
class Prod_Config(Config):
DEBUG=False
ENV='production'
SQLALCHEMY_DATABASE_URI = (
'mysql+pymysql://{user}:{password}@localhost/{database}'
'?unix_socket=/cloudsql/{connection_name}').format(
user=USER, password=PASSWORD,
database=DATABASE, connection_name=CONNECTION_NAME)
config={'development': Dev_Config,
'production': Prod_Config
错误:
Traceback (most recent call last):
File "/env/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
File "/env/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 92, in init_process
super().init_process()
File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 119, in init_process
self.load_wsgi()
File "/env/lib/python3.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
self.wsgi = self.app.wsgi()
File "/env/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
return self.load_wsgiapp()
File "/env/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
return util.import_app(self.app_uri)
File "/env/lib/python3.7/site-packages/gunicorn/util.py", line 358, in import_app
mod = importlib.import_module(module)
File "/opt/python3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/srv/main.py", line 39, in <module>
admin.add_view(ModelView(User, db.session))
AttributeError: 'User' object has no attribute 'add_view'
知道为什么会这样吗?
您正在将 admin
的值从 class Admin
(admin = Admin(...)
) 覆盖到 User
(admin = User(...)
)。
最好不要在代码中为不同的目的使用相同的变量名。