Article Directory
1 Introduction
1.1 flask
Flask As a microframework, Flask allows you to build web services with very little overhead. It gives you, the designer, the freedom to implement your project-specific application in a way that suits you.
A minimal Flask application looks like this:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
1.2 flask-admin
Flask-Admin is a batteries-included, easy-to-use Flask extension that lets you add an admin interface to your Flask application. It was inspired by the django-admin package, but implemented in such a way that the developer has full control over the look, feel and functionality of the resulting application. Out of the box, Flask-Admin works well with a wide variety of ORMs, including:
- SQLAlchemy,
- MongoEngine,
- pymongo and
- Peewee.
The biggest feature of Flask-Admin is flexibility. It aims to provide a set of management interfaces that can be used to build any complexity. So, first, you can immediately create a very simple application that automatically generates CRUD views for each model. However, you can go a step further and customize these views and forms. Appears as needed.
-
In the world of microservices and APIs, Flask-Admin solves existing data models for building admin interfaces on top. Effortlessly, it lets you manage data for Web services through a user-friendly interface.
-
The basic concept behind Flask-Admin is that it lets you build complex interfaces by grouping individual views together in classes: each web page you see on the frontend represents a class that has been explicitly added to the interface Methods.
-
These view classes are especially useful when they are bound to specific database models, as they allow you to combine all the usual create, read, update, delete (CRUD) view logic into a single self-contained class for each model .
2. Installation
2.1 Install the library
Install via pip as follows:
pip install flask-admin
2.2 Print library version
Print the version information of the flask-admin library:
Method 1:
## 格式:pip show 库名
pip show flask-admin
Method 2:
## 格式:pip list
pip list flask-admin
Method 3:
## 格式:库名.__version__
import flask_admin
print('flask_admin版本:',flask_admin.__version__)
3. Initialization
The first step is to initialize an empty admin interface for the Flask application:
from flask import Flask
from flask_admin import Admin
app = Flask(__name__)
# set optional bootswatch theme
app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'
admin = Admin(app, name='爱看书的小沐', template_mode='bootstrap3')
# Add administrative views here
app.run()
After running the above script, the command line outputs the following information:
Open browser access:
http://127.0.0.1:5000/
Try another address:
http://127.0.0.1:5000/admin
The page is finally displayed normally.
This line in the code above defines.
admin = Admin(app, name='爱看书的小沐', template_mode='bootstrap3')
It can be divided into multiple lines as follows.
admin = Admin(name='爱看书的小沐')
# Add views here
admin.init_app(app)
4. Add an independent view (BaseView)
4.1 Management interface index page
- test.py
from flask import Flask
from flask_admin import Admin, BaseView, AdminIndexView, expose
app = Flask(__name__)
# set optional bootswatch theme
app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['BABEL_DEFAULT_LOCALE'] = 'zh_CN'
# AdminIndexView:当访问/admin/url时,默认的管理接口索引页;它可以通过将你自己的视图类传递给Admin构造函数来重写
## index_view = AdminIndexView(name=u'首页', template='index.html')
## admin = Admin(app, name=u"后台管理系统", template_mode='bootstrap3', index_view=index_view)
class MyAdminIndexView(AdminIndexView):
@expose("/")
def homepage(self):
return self.render("index.html")
admin = Admin(
app,
name=u"后台管理系统",
index_view=MyAdminIndexView(name="预览页"),
template_mode='bootstrap3'
)
app.run()
- index.html
{% extends 'admin/master.html' %}
{% block body %}
Hello World from 爱看书的小沐!
{% endblock %}
4.2 Custom view
① Each custom view must provide an @expose('/') index method, otherwise an error will be reported
② Multiple independent view classes can be defined, and then defined into the same type
import os.path as op
from flask import Flask
from flask_admin import Admin, BaseView, AdminIndexView, expose
app = Flask(__name__)
# set optional bootswatch theme
app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['BABEL_DEFAULT_LOCALE'] = 'zh_CN'
# AdminIndexView:当访问/admin/url时,默认的管理接口索引页;它可以通过将你自己的视图类传递给Admin构造函数来重写
class MyAdminIndexView(AdminIndexView):
@expose("/")
def homepage(self):
return self.render("admin/index.html")
admin = Admin(
app,
name=u"后台管理系统",
index_view=MyAdminIndexView(name="预览页"),
template_mode='bootstrap3'
)
#admin = Admin(
# app,
# index_view=AdminIndexView(
# name='导航栏',
# template='welcome.html',
# url='/admin'
# )
#)
class MyView1(BaseView):
@expose('/')
def main(self):
return self.render('admin/oneview.html')
class MyView2(BaseView):
@expose('/')
def main(self):
return self.render('admin/twoview.html')
class MyView3(BaseView):
@expose('/')
def index(self):
return self.render('admin/index.html')
@expose('/test/')
def test(self):
return self.render('admin/index.html')
# 增加独立视图
admin.add_view(MyView1(name='页面一', category='测试视图', endpoint="/onepage/"))
admin.add_view(MyView2(name='页面二', category='测试视图', endpoint="/twopage/"))
admin.add_view(MyView3(name='页面三'))
admin.add_view(MyView3(name='Hello 1', category='测试视图2', endpoint='test1'))
admin.add_view(MyView3(name='Hello 2', category='测试视图2', endpoint='test2'))
admin.add_view(MyView3(name='Hello 3', category='测试视图2', endpoint='test3'))
app.run()
class MyNews(BaseView):
@expose('/', methods=['GET', 'POST'])
def index(self):
form = NameForm()
return self.render('donews.html', form=form)
admin.add_view(MyNews(name=u'发表新闻'))
5. Add model view (ModelView)
Model Views allow you to add a dedicated set of admin pages for managing any model in your database. By creating an instance of the ModelView class, you can import it from one of Flask-Admin's built-in ORM backends.
import os.path as op
from flask import Flask
from flask_admin import Admin, BaseView, AdminIndexView, expose
from flask_admin.contrib.sqla import ModelView
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
# set optional bootswatch theme
app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'
app.config['DEBUG'] = True
app.config['BABEL_DEFAULT_LOCALE'] = 'zh_CN'
app.config['SQLALCHEMY_ECHO'] = True
# app.config['SECRET_KEY'] = 'super-secret'
# app.config['SECURITY_PASSWORD_HASH'] = 'pbkdf2_sha512'
# app.config['SECURITY_PASSWORD_SALT'] = '16a0af319890f662055ba10aecff37e7e033db3fba737e55'
# app.config['SECURITY_USER_IDENTITY_ATTRIBUTES'] = 'email'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@localhost:3306/flask_test'
db = SQLAlchemy(app)
admin = Admin(app)
class Users(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
created_at = db.Column(db.TIMESTAMP)
updated_at = db.Column(db.TIMESTAMP)
username = db.Column(db.String(128))
email = db.Column(db.String(128))
nickname = db.Column(db.String(128))
avatar = db.Column(db.String(255))
password_hash = db.Column(db.String(128))
status = db.Column(db.Integer)
class MyView(ModelView):
# Disable model creation
can_create = False
# Override displayed fields
column_list = ('username', 'email')
def __init__(self, session, **kwargs):
# You can pass name and other parameters if you want to
super(MyView, self).__init__(Users, session, **kwargs)
admin.add_view(MyView(db.session))
# admin.add_view(ModelView(Users, db.session))
app.run()
The column names are Chinese, that is, column_labels needs to be rewritten.
class MyView(ModelView):
# Disable model creation
can_create = False
# Override displayed fields
column_list = ('id', 'username', 'email')
column_labels = {
'id': u'序号',
'username' : u'用户名',
'email':u'电子邮箱',
}
def __init__(self, session, **kwargs):
# You can pass name and other parameters if you want to
super(MyView, self).__init__(Users, session, **kwargs)
6. Add special view
6.1 Managing Files & Folders
from flask import Flask
from flask_admin import Admin
from flask_admin.contrib.fileadmin import FileAdmin
import os.path as op
app = Flask(__name__)
# set optional bootswatch theme
app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'
admin = Admin(app, name='爱看书的小沐', template_mode='bootstrap3')
path = op.join(op.dirname(__file__), 'static')
admin.add_view(FileAdmin(path, '/static/', name='文件管理'))
app.run()
Add parameter control as follows:
import os.path as op
from flask import Flask
from flask_admin import Admin
from flask_admin.contrib.fileadmin import FileAdmin
from flask_babelex import Babel
app = Flask(__name__)
# set optional bootswatch theme
app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['BABEL_DEFAULT_LOCALE'] = 'zh_CN'
babel = Babel(app)
admin = Admin(app, name='爱看书的小沐', template_mode='bootstrap3')
class MyAdmin(FileAdmin):
can_upload = False
can_download = False
can_delete = False
can_delete_dirs = False
can_mkdir = False
can_rename = False
allowed_extensions = ('swf', 'jpg', 'gif', 'png')
path = op.join(op.dirname(__file__), 'static')
admin.add_view(MyAdmin(path, '/static/', name='文件管理'))
app.run()
6.2 Adding A Redis Console
Another plugin available is Redis Console. If you have a Redis instance running on the same computer as your application, you can:
from flask import Flask
from flask_admin import Admin
from redis import Redis
from flask_admin.contrib import rediscli
app = Flask(__name__)
# set optional bootswatch theme
app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
admin = Admin(app, name='爱看书的小沐', template_mode='bootstrap3')
# Flask setup here
admin.add_view(rediscli.RedisCli(Redis()))
app.run()
7. Chinese support
from flask_babelex import Babel
# 使用flask_babelex可以显示中文,该模块用于做国际化
babel = Babel(app)
There are two ways to support displaying Chinese, which are as follows:
① App settings are added as follows: BABEL_DEFAULT_LOCALE = "zh_CN"
app.config['BABEL_DEFAULT_LOCALE'] = 'zh_CN'
② Add the following method
from flask import request, session
# 如下方法也可以定义中文展示,其效果类似于BABEL_DEFAULT_LOCALE = "zh_CN"
@babel.localeselector
def get_locale():
if request.args.get('lang'):
session['lang'] = request.args.get('lang')
return session.get('lang', 'zh_Hans_CN')
8. Authentication
Flask-Admin doesn't envision any authentication system you can use. Therefore, the default management interface is completely open.
To control access to the admin interface, you can specify the is_accessible method when extending the BaseView class. So, for example, if you use Flask-Login for authentication, the following code ensures that only logged-in users can access the view:
class MyView(BaseView):
def is_accessible(self):
return login.current_user.is_authenticated()
Generally, the background is not open to all users, and all involve administrator privileges. I rely on the flask-login extension to achieve this.
class MyView(ModelView):
def is_accessible(self):
if current_user.is_authenticated and current_user.username == "admin":
return True
return False
# Disable model creation
can_create = False
# Override displayed fields
column_list = ('id', 'username', 'email')
column_labels = {
'id': u'序号',
'username' : u'用户名',
'email':u'电子邮箱',
}
def __init__(self, session, **kwargs):
# You can pass name and other parameters if you want to
super(MyView, self).__init__(Users, session, **kwargs)
At the moment the user management tab is hidden.
epilogue
如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;
╮( ̄▽ ̄)╭
如果您感觉方法或代码不咋地
//(ㄒoㄒ)// ,就在评论处留言,作者继续改进;
o_O???
如果您需要相关功能的代码定制化开发,可以留言私信作者;
(✿◡‿◡)
感谢各位大佬童鞋们的支持!
(´▽´)ノ (´▽´)! ! !