flask_admin 筆記五 內置模板設置

內建模板css

Flask-Admin是使用jinja2模板引擎html

1)擴展內建的模板git

   不要徹底覆蓋內置的模板,最好是擴展它們。 這將使您更容易升級到新的Flask-Admin版本。github

 

在內部,Flask-Admin模板是從admin / master.html模板派生的。 三個最有趣的擴展模板多是:flask

  • admin/model/list.html
  • admin/model/create.html
  • admin/model/edit.html

爲了擴展默認的編輯模板功能,你能夠建立一個模板templates/micrblog_edit.html 去代替它:api

{% extends 'admin/model/edit.html' %}

 
{% block body %}

    <h1>MicroBlog Edit View</h1>

    {{ super() }}

{% endblock %}

接着,須要視圖類使用這個模板,需設置屬性:app

class MicroBlogModelView(ModelView):
    edit_template = 'microblog_edit.html'
    # create_template = 'microblog_create.html'
    # list_template = 'microblog_list.html'
 

若是您想使用本身的基本模板,則在初始化期間將模板的名稱傳遞給管理構造函數:函數

admin = Admin(app, base_template='microblog_master.html')

 

2)重寫內置的模板佈局

要徹底控制管理界面的樣式和佈局,能夠覆蓋全部內置模板。 請記住,從Flask-Admin的一個版本到下一個版本的模板會略有變化,因此一旦你開始覆蓋它們,你須要當心升級你的軟件包版本。url

 

要覆蓋任何內置模板,只需將它們從Flask-Admin源代碼複製到項目的templates/admin /目錄中便可。 只要文件名保持不變,項目目錄中的模板應該自動優先於內置模板。

 

3)可用的模板塊

Flask-Admin在admin / master.html中定義了一個基本模板,即全部其餘管理模板都是從其派生的。 該模板是指向admin / base.html的代理,它定義瞭如下塊:

模塊名

描述

Head_meta

頭部標籤

title

標題

Head_css

頭部存放樣式

head

頭部其餘內容

Page_body

頁面佈局

brand

菜單的商標

Main_menu

菜單

Menu_links

菜單連接

Access_control

菜單右邊的登陸按鈕

message

提醒的信息

body

內容區

tail

內容區下面的區域

除了從admin / master.html繼承的全部塊以外,admin / model / list.html模板還包含如下塊:

Block Name

Description

model_menu_bar

Menu bar

model_list_table

Table container

list_header

Table header row

list_row_actions_header

Actions header

list_row

Single row

list_row_actions

Row action cell with edit/remove/etc buttons

empty_list_message

Message that will be displayed if there are no models found

查看https://github.com/flask-admin/flask-admin/tree/master/examples/layout上的佈局示例,瞭解如何對管理界面進行全面的風格控制。

 

4)環境變量

在擴展admin / master.html的任何模板中工做時,您均可以訪問少許的環境變量:

Variable Name

Description

admin_view

Current administrative view

admin_base_template

Base template name

_gettext

Babel gettext

_ngettext

Babel ngettext

h

Helpers from helpers module

 

5)產生url連接

要生成特定視圖的網址,請使用帶點前綴的url_for:

from flask import url_for
class MyView(BaseView):
    @expose('/')
    def index(self):
        # Get URL for the test view method
        user_list_url = url_for('user.index_view')
        return self.render('index.html', user_list_url=user_list_url)

一個特定的記錄也能夠引用:

# Edit View for record #1 (redirect back to index_view)
url_for('user.edit_view', id=1, url=url_for('user.index_view'))

引用ModelView實例時,在調用url_for時使用模型的小寫名稱做爲前綴。 其餘視圖能夠經過爲每一個視圖指定惟一的端點並將其用做前綴來引用。 因此,你可使用:

url_for('analytics.index')

指向如下的視圖:

admin.add_view(CustomView(name='Analytics', endpoint='analytics'))
相關文章
相關標籤/搜索