flask-admin

How do I properly set up flask-admin views with using an application factory?

怎甘沉沦 提交于 2019-12-04 16:34:18
I'm trying to setup flask-admin model views with SQLAlchemy against 'user' and 'role' models. Instead of a function admin view I'm getting: ValueError: Invalid model property name <class 'app.models.Role'>.desc Stack trace: Traceback (most recent call last): File "/Users/dbg/Projects/Python/Current/ziff/flaskbase/manage.py", line 18, in <module> app = create_app(os.getenv('APP_CONFIG') or 'default') File "/Users/dbg/Projects/Python/Current/ziff/flaskbase/app/__init__.py", line 49, in create_app admin.add_view(RoleAdmin(Role, db.session)) File "/usr/local/share/anaconda/envs/flaskbase27/lib

How do I add flask-admin to a Blueprint?

℡╲_俬逩灬. 提交于 2019-12-04 09:57:59
问题 for example: from flask import Flask from flask.ext.admin import Admin, BaseView, expose class MyView(BaseView): @expose('/') def index(self): return self.render('index.html') app = Flask(__name__) admin = Admin(app) admin.add_view(MyView(name='Hello')) app.run() but, if I need a new file, called 'views.py', how can I add a view into views.py to admin? Do I need to use a blueprint? 回答1: For my project I actually made a child class of Blueprint that supports flask admin: from flask import

Flask-Security login and logout in menu bar

☆樱花仙子☆ 提交于 2019-12-04 06:43:34
问题 Currently I use the following code to display the login and logout links on the menu bar in my Flask-Admin project: admin.add_link(MenuLink(name='Logout', category='', url="/logout")) admin.add_link(MenuLink(name='Login', category='', url="/login")) However, this will display both regardless of whether the current user is logged in or not. Is it possible to get it to display logout when logged in and login when logged out? 回答1: Looking at the menu template definitions in Flask-Admin (layout

Flask-Admin Role Based Access - Modify access based on role

北战南征 提交于 2019-12-04 05:39:43
I took the Flask-Admin auth example from here and changed it slightly. I added the following block to the view below, but it doesn't show the export button. I was expecting it to add the export option to the admin views. It does print ---superuser to the console. if current_user.has_role('superuser'): can_export = True print ' ---- superuser ' I have used the export feature many times before. It will work if I put the statement can_export = True just below class MyModelView(sqla.ModelView): I am using this as an example of controlling access to creating/editing/etc based on the user role. For

flask-admin

十年热恋 提交于 2019-12-04 04:19:30
一、安装 pip3 install flask_admin 二、简单使用 from flask import Flask from flask_admin import Admin app = Flask(__name__) #将app注册到adminzhong admin = Admin(app) if __name__=="mian": app.run() #访问 #127.0.0.1:5000/admin端口,会得到一个空白的页面 三、将表模型注册到admin中 #在将表注册之前应该对app进行配置 SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:@127.0.0.1:3307/py9api?charset=utf8mb4" SQLALCHEMY_POOL_SIZE = 5 SQLALCHEMY_POOL_TIMEOUT = 30 SQLALCHEMY_POOL_RECYCLE = -1 #导入models文件的中的表模型 from flask_admin.contrib.sqla import ModelView from api.models import Stock,Product,Images,Category,Wxuser,Banner admin.add_view(ModelView(Stock, db

Wrong dashboard while adding flask-admin to project

倖福魔咒の 提交于 2019-12-04 02:55:02
I'm trying to extend the flask-base project https://github.com/hack4impact/flask-base/tree/master/app . This uses the the application factory pattern in app/init.py and blueprints. In the app/init.py I have: import os from flask import Flask from flask_mail import Mail from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager from flask_assets import Environment from flask_wtf import CsrfProtect from flask_compress import Compress from flask_rq import RQ from flask_admin import Admin, BaseView, expose from flask_admin.contrib.sqla import ModelView # from app.models import

limit choices with dropdown in flask-admin

坚强是说给别人听的谎言 提交于 2019-12-03 16:32:36
问题 My SQLAlchemy model has a String field that I would like to limit to a few choices. I am wondering how I would be able to create a dropdown for this field in the Flask-Admin interface that would ensure that the db is only populated with one of my choices. If I let the user manually enter these fields, they may spell them incorrectly, etc. 回答1: enum , form_choices and form_args Your question is about restricting values at the form level, but we can also briefly discuss it at the schema level.

How to handle ordered many-to-many relationship (association proxy) in Flask-Admin form?

↘锁芯ラ 提交于 2019-12-03 16:22:22
I have a many-to-many relationship between declarative models Page and Survey , which is mediated by association proxies because the order in which pages appear in a survey is important, so the cross-linking table has an additional field. from flask.ext.sqlalchemy import SQLAlchemy from sqlalchemy.ext.associationproxy import association_proxy db = SQLAlchemy() class Page (db.Model): id = db.Column(db.Integer, primary_key = True) surveys = association_proxy('page_surveys', 'survey') class Survey (db.Model): id = db.Column(db.Integer, primary_key = True) pages = association_proxy('survey_pages',

Wrong dashboard while adding flask-admin to project

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to extend the flask-base project https://github.com/hack4impact/flask-base/tree/master/app . This uses the the application factory pattern in app/init.py and blueprints. In the app/init.py I have: import os from flask import Flask from flask_mail import Mail from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager from flask_assets import Environment from flask_wtf import CsrfProtect from flask_compress import Compress from flask_rq import RQ from flask_admin import Admin, BaseView, expose from flask_admin

flask-script、flask-admin组件

半城伤御伤魂 提交于 2019-12-03 07:42:29
目录 flask-script 安装 使用 自定制命令 flask-admin 安装 简单使用 将表模型注册到admin中 如果有个字段是图片字段 flask-script 用于实现类似于django中 python3 manage.py runserver ...类似的命令 安装 pip3 install flask-script 使用 from flask_script import Manager app = Flask(__name__) manager=Manager(app) ... if __name__ == '__main__': manager.run() #以后在执行,直接:python3 manage.py runserver #python3 manage.py runserver --help 自定制命令 @manager.command def custom(arg): """ 自定义命令 python manage.py custom 123 :param arg: :return: """ print(arg) @manager.option('-n', '--name', dest='name') #@manager.option('-u', '--url', dest='url') def cmd(name, url): """ 自定义命令(