Flask-Admin user guide

Source: Internet
Author: User
Tags tojson subdomain
Flask-Admin is a micro-framework serving the Python Flask framework. it can generate a Model-level data management interface for users like Django-Admin, next, let's take a look at the Flask graphic management interface building framework. the Flask-Admin tutorial is an extension of the Flask framework, which allows you to quickly create a Web management interface, it provides common management functions such as adding, deleting, modifying, and querying users and files. if you do not like its default interface, you can modify the template file to customize it;
Flask-Admin regards each menu (hyperlink) as a view, which can be displayed only after registration. The view itself also has attributes to control whether it is visible. therefore, you can use this mechanism to customize your own modular interface. for example, users with different permissions can see different menus after logon;

Project address: https://flask-admin.readthedocs.io/en/latest/

Example/simple
This is the simplest example. it helps us quickly and intuitively understand basic concepts and learn to customize the Flask-Admin interface.
Simple. py:

from flask import Flaskfrom flask.ext import admin# Create custom admin viewclass MyAdminView(admin.BaseView):  @admin.expose('/')  def index(self):    return self.render('myadmin.html')class AnotherAdminView(admin.BaseView):  @admin.expose('/')  def index(self):    return self.render('anotheradmin.html')  @admin.expose('/test/')  def test(self):    return self.render('test.html')# Create flask appapp = Flask(__name__, template_folder='templates')app.debug = True# Flask views@app.route('/')def index():  return 'Click me to get to Admin!'# Create admin interfaceadmin = admin.Admin()admin.add_view(MyAdminView(category='Test'))admin.add_view(AnotherAdminView(category='Test'))admin.init_app(app)if __name__ == '__main__':  # Start app  app.run()

Here you can see the running effect

BaseView

All views must be inherited from BaseView:

The code is as follows:


Class BaseView (name = None, category = None, endpoint = None, url = None, static_folder = None, static_url_path = None)


Name: view is displayed as a menu (hyperlink) on the page. menu name = 'name'. The lower-case class name is used by default.
Category: if multiple views share the same category, they are all put into one dropdown (dropdown name = 'Category ')
Endpoint: If the endpoint is 'XXX', you can use url_for (xxx. index) or change the page URL (/admin/xxx)
Url: page URL, priority url> endpoint> class name
Static_folder: Path of the static Directory
Static_url_path: URL of the static Directory
Anotheradmin.html:

{% extends 'admin/master.html' %}{% block body %}  Hello World from AnotherMyAdmin!
Click me to go to test view{% endblock %}

If AnotherAdminView adds the parameter endpoint = 'XXX', you can write it as url_for ('XXX. text'). then, the page URL changes from/admin/anotheradminview/to/admin/xxx.
If the parameter url = 'AAA' is specified at the same time, the page URL will be/admin/aaa, and the url priority is higher than the endpoint
Admin

The code is as follows:


Class Admin (app = None, name = None, url = None, subdomain = None, index_view = None, translations_path = None, endpoint = None, static_url_path = None, base_template = None)


App: Flask Application Object. In this example, admin. init_app (app) can be left empty, and admin = admin. Admin (app = app) is the same.
Name: Application name. The default value is 'admin'. it is displayed as the main menu name ('admin' on the left of 'home') and page title.
Subdomain :???
Index_view: The menu corresponding to 'home' is called index view. the default value is AdminIndexView.
Base_template: basic template. the default template is admin/base.html. the template is in the source code directory of Flask-Admin.
Some Admin code is as follows:

class MenuItem(object):  """    Simple menu tree hierarchy.  """  def __init__(self, name, view=None):    self.name = name    self._view = view    self._children = []    self._children_urls = set()    self._cached_url = None    self.url = None    if view is not None:      self.url = view.url  def add_child(self, view):    self._children.append(view)    self._children_urls.add(view.url)class Admin(object):  def __init__(self, app=None, name=None,         url=None, subdomain=None,         index_view=None,         translations_path=None,         endpoint=None,         static_url_path=None,         base_template=None):    self.app = app    self.translations_path = translations_path    self._views = []    self._menu = []    self._menu_categories = dict()    self._menu_links = []    if name is None:      name = 'Admin'    self.name = name    self.index_view = index_view or AdminIndexView(endpoint=endpoint, url=url)    self.endpoint = endpoint or self.index_view.endpoint    self.url = url or self.index_view.url    self.static_url_path = static_url_path    self.subdomain = subdomain    self.base_template = base_template or 'admin/base.html'    # Add predefined index view    self.add_view(self.index_view)    # Register with application    if app is not None:      self._init_extension()  def add_view(self, view):    # Add to views    self._views.append(view)    # If app was provided in constructor, register view with Flask app    if self.app is not None:      self.app.register_blueprint(view.create_blueprint(self))      self._add_view_to_menu(view)  def _add_view_to_menu(self, view):    if view.category:      category = self._menu_categories.get(view.category)      if category is None:        category = MenuItem(view.category)        self._menu_categories[view.category] = category        self._menu.append(category)      category.add_child(MenuItem(view.name, view))    else:      self._menu.append(MenuItem(view.name, view))  def init_app(self, app):    self.app = app    self._init_extension()    # Register views    for view in self._views:      app.register_blueprint(view.create_blueprint(self))      self._add_view_to_menu(view)

From the code above, we can see that init_app (app) and Admin (app = app) are the same:
Register each view as blueprint (the concept in Flask can be simply understood as a module)
Record all views and their category and url
AdminIndexView

The code is as follows:


Class AdminIndexView (name = None, category = None, endpoint = None, url = None, template = 'admin/index.html ')


Name: 'Home' by default'
Endpoint: 'admin' by default'
Url: The default value is '/admin'
If you want to encapsulate your own view, follow the instructions in AdminIndexView:

class AdminIndexView(BaseView):  def __init__(self, name=None, category=None,         endpoint=None, url=None,         template='admin/index.html'):    super(AdminIndexView, self).__init__(name or babel.lazy_gettext('Home'),                       category,                       endpoint or 'admin',                       url or '/admin',                       'static')    self._template = template  @expose()  def index(self):    return self.render(self._template)base_template

Base_template is/admin/base.html by default, and is the main code of the page (based on bootstrap). It also imports admin/layout.html;
Layout is a macro used to expand and display menus;
Use some variables in the template to retrieve the information saved during previous view registration (such as menu name and url ):
# Admin/layout.html (part)

{% macro menu() %} {% for item in admin_view.admin.menu() %}  {% if item.is_category() %}   {% set children = item.get_children() %}   {% if children %}    {% if item.is_active(admin_view) %}
  • {% else %}
  • {% endif %} {{ item.name }}
      {% for child in children %} {% if child.is_active(admin_view) %}
    • {% else %}
    • {% endif %} {{ child.name }}
    • {% endfor %}
  • {% endif %} {% else %} {% if item.is_accessible() and item.is_visible() %} {% if item.is_active(admin_view) %}
  • {% else %}
  • {% endif %} {{ item.name }}
  • {% endif %} {% endif %} {% endfor %}{% endmacro %}

    Example/file
    This example can help us quickly build a file management interface, but our focus is on learning to use the ActionsMixin module.
    File. py:

    import osimport os.path as opfrom flask import Flaskfrom flask.ext import adminfrom flask.ext.admin.contrib import fileadmin# Create flask appapp = Flask(__name__, template_folder='templates', static_folder='files')# Create dummy secrey key so we can use flashapp.config['SECRET_KEY'] = '123456790'# Flask views@app.route('/')def index():  return 'Click me to get to Admin!'if __name__ == '__main__':  # Create directory  path = op.join(op.dirname(__file__), 'files')  try:    os.mkdir(path)  except OSError:    pass  # Create admin interface  admin = admin.Admin(app)  admin.add_view(fileadmin.FileAdmin(path, '/files/', name='Files'))  # Start app  app.run(debug=True)

    FileAdmin is a written view, which can be used directly:

    The code is as follows:


    Class FileAdmin (base_path, base_url, name = None, category = None, endpoint = None, url = None, verify_path = True)


    Base_path: relative path of the file
    Base_url: URL of the file directory
    The code in FileAdmin and ActionsMixin is as follows:
    Class FileAdmin (BaseView, ActionsMixin ):

    Def _ init _ (self, base_path, base_url, name = None, category = None, endpoint = None, url = None, verify_path = True): self. init_actions () @ expose ('/action/', methods = ('post',) def action_view (self): return self. handle_action () # Actions @ action ('delete', lazy_gettext ('delete'), lazy_gettext ('Are you sure you want to delete these files? ') Def action_delete (self, items): if not self. can_delete: flash (gettext ('file deletion is disabled. '), 'error') return for path in items: base_path, full_path, path = self. _ normalize_path (path) if self. is_accessible_path (path): try: OS. remove (full_path) flash (gettext ('file "% (name) s" was successfully deleted. ', name = path) Failed t Exception as ex: flash (gettext ('failed to delete file: % (name) s', name = ex), 'error ') @ action ('edit', lazy_gettext ('Edit') def action_edit (self, items): return redirect (url_for ('. edit', path = items) @ action () is used for the function after wrap. the function here is to save the parameter: def action (name, text, confirmation = None) def wrap (f): f. _ action = (name, text, confirmation) return f return wrap

    Name: action name
    Text: the name of a button.
    Confirmation: confirmation information in the pop-up box
    Init_actions () saves all action information to ActionsMixin:

    # Debugging information _ actions = [('delete', lu 'delete'), ('edit', lu 'edit')] _ actions_data = {'edit ':(
     
      
    >, Lu 'edit', None), 'delete ':(
      
       
    >, Lu 'delete', lu 'Are you sure you want to Delete these files? ')}
      
     

    Action_view () is used to process the POST request to/action/, and then calls handle_action (). it then calls different actions for processing, and finally returns the current page:

    # Omitting irrelevant code def handle_action (self, return_view = None): action = request. form. get ('action') ids = request. form. getlist ('rowid') handler = self. _ actions_data.get (action) if handler and self. is_action_allowed (action): response = handler [0] (ids) if response is not None: return response if not return_view: url = url_for ('. '+ self. _ default_view) else: url = url_for ('. '+ return_view) return redirect (url)

    Ids is a file list that is passed as a parameter to the action handler (parameter items ):

    # Debugging information ids: [u'1.png ', u'2.png']

    Analyze the page code. the file corresponding to the Files page is admin/file/list.html. focus on the code related to the With selected drop-down menu:
    {% Import 'admin/actions.html 'as actionslib with context %}

    {% if actions %}  

    {{ actionslib.dropdown(actions, 'dropdown-toggle btn btn-large') }}

    {% endif %}{% block actions %} {{ actionslib.form(actions, url_for('.action_view')) }}{% endblock %}{% block tail %} {{ actionslib.script(_gettext('Please select at least one file.'), actions, actions_confirmation) }}{% endblock %}

    The following three macros are used in actions.html:

    {% Macro dropdown (actions, btn_class = 'dropdown-toggle ')-%} {_ gettext (' With selected ')}}  
     
     
      {% For p in actions %}
    • {_ Gettext (p [1])}
    • {% Endfor %}
    {% Endmacro % }{% macro form (actions, url) % }{% if actions % }{% endif % }{% endmacro % }{% macro script (message, actions, actions_confirmation) %} {% if actions %}

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.