Flask-Admin - 添加自定义视图
Flask-Admin - adding a custom view
我正在使用 Flask-Admin 开发网络应用程序。
使用 ModelView 时一切顺利,但在子类化 BaseView 时我根本无法让它工作。
我正在尝试添加自己的视图,以便显示来自数据库的不同数据。
还有我在使用BaseView时无法调用数据库
这是我的代码:
app/__init__.py
#!/flask/bin/python
from flask import Flask
app = Flask(__name__)
app.config.from_object('config')
app/models.py
#!/flask/bin/python
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, BOOLEAN
from sqlalchemy.orm import relationship
import random
import string
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
name = Column(String(32))
token = Column(String(16), primary_key=True)
originator = Column(String(12))
active = Column(BOOLEAN, default=True)
logging = Column(BOOLEAN)
def __init__(self, name='', token='no_token', account='test', originator='Test', active=True, logging=0):
self.name = name
self.token = token
self.originator = originator
self.active = active
self.logging = logging
def __str__(self):
return self.name
app/views.py
#imports ommited
from app.models import User
#this works totally fine
class UserView(ModelView):
form_columns = ('name', 'originator','account_type')
column_display_pk=True
column_searchable_list = ('name')
form_excluded_columns = ('logging')
def __init__(self, session, **kwargs):
super(UserView, self).__init__(models.User, session, **kwargs)
class CRUDSettings(BaseView):
@expose('/')
def index(self):
#HERE I don't have access to my User DB object.
I tried with db.session.query(User).all() - db is undefined
I tried many other ways but I can't get all users so I can populate the select dropdown box.
users = User <----------------------------- ?
return self.render('crud.html', accounts = users)
templates/crud.html
{% extends 'admin/master.html' %}
{% block body %}
<div class="row">
<form action="/" id="form_acc" name="form_acc" method="POST">
<label id="lbl_acc">Account:</label>
<select name="d_account" id="d_account">
{% for acc in accounts %}
<option value="{{acc}}" {{'selected' if selected_account==acc else ''}}>{{acc}}</option>
{% endfor %}
</select>
<button class="button " id="brand_submit" type="submit">Go</button>
</form>
</div>
{% endblock %}
run.py
#!/flask/bin/python
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.admin import Admin
from app import views
app = Flask(__name__)
SERVER = ''
PORT = ''
DATABASE = ''
USERNAME = ''
PASSWORD = ''
SSL = ''
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s:%s/%s?%s' % (
USERNAME, PASSWORD, SERVER, PORT, DATABASE, SSL)
db = SQLAlchemy(app)
if __name__ == '__main__':
admin = Admin(app)
admin.add_view(views.UserView(db.session))
admin.add_view(views.CRUDSettings(name='CRUD Settings', endpoint='crud'))
app.config['SESSION_TYPE'] = 'filesystem'
app.run('127.0.0.1', 5001, debug=True)
调用 CRUDSettings
构造函数时传递 app db
变量。将您的视图模型重构为如下所示:
class CRUDSettings(BaseView):
def __init__(self, session, name=None, category=None, endpoint=None, url=None, static_folder=None,
menu_class_name=None, menu_icon_type=None, menu_icon_value=None):
self.session = session
super(CRUDSettings, self).__init__(name, category, endpoint, url, static_folder,
menu_class_name=menu_class_name, menu_icon_type=menu_icon_type,
menu_icon_value=menu_icon_value)
@expose('/')
def index(self):
# users = self.session.query(User).all()
现set-up您的看法如下:
admin.add_view(views.CRUDSettings(session=db.session, name='CRUD Settings', endpoint='crud'))
我正在使用 Flask-Admin 开发网络应用程序。
使用 ModelView 时一切顺利,但在子类化 BaseView 时我根本无法让它工作。
我正在尝试添加自己的视图,以便显示来自数据库的不同数据。
还有我在使用BaseView时无法调用数据库
这是我的代码:
app/__init__.py
#!/flask/bin/python
from flask import Flask
app = Flask(__name__)
app.config.from_object('config')
app/models.py
#!/flask/bin/python
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, BOOLEAN
from sqlalchemy.orm import relationship
import random
import string
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
name = Column(String(32))
token = Column(String(16), primary_key=True)
originator = Column(String(12))
active = Column(BOOLEAN, default=True)
logging = Column(BOOLEAN)
def __init__(self, name='', token='no_token', account='test', originator='Test', active=True, logging=0):
self.name = name
self.token = token
self.originator = originator
self.active = active
self.logging = logging
def __str__(self):
return self.name
app/views.py
#imports ommited
from app.models import User
#this works totally fine
class UserView(ModelView):
form_columns = ('name', 'originator','account_type')
column_display_pk=True
column_searchable_list = ('name')
form_excluded_columns = ('logging')
def __init__(self, session, **kwargs):
super(UserView, self).__init__(models.User, session, **kwargs)
class CRUDSettings(BaseView):
@expose('/')
def index(self):
#HERE I don't have access to my User DB object.
I tried with db.session.query(User).all() - db is undefined
I tried many other ways but I can't get all users so I can populate the select dropdown box.
users = User <----------------------------- ?
return self.render('crud.html', accounts = users)
templates/crud.html
{% extends 'admin/master.html' %}
{% block body %}
<div class="row">
<form action="/" id="form_acc" name="form_acc" method="POST">
<label id="lbl_acc">Account:</label>
<select name="d_account" id="d_account">
{% for acc in accounts %}
<option value="{{acc}}" {{'selected' if selected_account==acc else ''}}>{{acc}}</option>
{% endfor %}
</select>
<button class="button " id="brand_submit" type="submit">Go</button>
</form>
</div>
{% endblock %}
run.py
#!/flask/bin/python
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.admin import Admin
from app import views
app = Flask(__name__)
SERVER = ''
PORT = ''
DATABASE = ''
USERNAME = ''
PASSWORD = ''
SSL = ''
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s:%s/%s?%s' % (
USERNAME, PASSWORD, SERVER, PORT, DATABASE, SSL)
db = SQLAlchemy(app)
if __name__ == '__main__':
admin = Admin(app)
admin.add_view(views.UserView(db.session))
admin.add_view(views.CRUDSettings(name='CRUD Settings', endpoint='crud'))
app.config['SESSION_TYPE'] = 'filesystem'
app.run('127.0.0.1', 5001, debug=True)
调用 CRUDSettings
构造函数时传递 app db
变量。将您的视图模型重构为如下所示:
class CRUDSettings(BaseView):
def __init__(self, session, name=None, category=None, endpoint=None, url=None, static_folder=None,
menu_class_name=None, menu_icon_type=None, menu_icon_value=None):
self.session = session
super(CRUDSettings, self).__init__(name, category, endpoint, url, static_folder,
menu_class_name=menu_class_name, menu_icon_type=menu_icon_type,
menu_icon_value=menu_icon_value)
@expose('/')
def index(self):
# users = self.session.query(User).all()
现set-up您的看法如下:
admin.add_view(views.CRUDSettings(session=db.session, name='CRUD Settings', endpoint='crud'))