使用Flask-Admin的自定义列表视图 [英] Custom list view with Flask-Admin

查看:87
本文介绍了使用Flask-Admin的自定义列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的模型Call,我正在使用Flask-Admin创建/编辑/删除该模型的实例.

I have a simple model Call and I'm using Flask-Admin to create/edit/delete instances of this model.

呼叫"字段之一是音频文件的路径.我希望能够通过添加一些html代码在管理员中播放文件.我检查了模板 flask_admin/templates/bootstrap3/admin/model/list.html ,看来执行我想要的唯一方法是在行的末尾添加一个单元格,这意味着扩展list.html,复制整个代码块 list_row 并添加我的单元格.

One of the field of Call is the path to an audio file. I would like to be able to play the file in the admin by adding some html code. I checked the template flask_admin/templates/bootstrap3/admin/model/list.html and it seems that the only way to do what I want is to add a cell at the end of the row, which means extending list.html, copying the whole block list_row and adding my cell.

这是唯一的方法吗?还是有什么方法可以将音频播放器(基本上是html5)的假"字段添加到表单中?

Is it the only way? Or is there any way to add a "fake" field with my audio player (basically an html5 ) to the form?

flask_admin/templates/bootstrap3/admin/model/list.html

....

{% for c, name in list_columns %}
    <td class="col-{{c}}">
    {% if admin_view.is_editable(c) %}
        {% if form.csrf_token %}
            {{ form[c](pk=get_pk_value(row), value=get_value(row, c), csrf=form.csrf_token._value()) }}
        {% else %}
            {{ form[c](pk=get_pk_value(row), value=get_value(row, c)) }}
        {% endif %}
    {% else %}
        {{ get_value(row, c) }}
    {% endif %}
    </td>
{% endfor %}
<td>ADD MY CUSTOM CELL HERE?</td>
....

models.py

class Call(db.Model):
    __tablename__ = 'calls'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode(64))
    path = db.Column(db.Unicode(128))

    def __unicode__(self):
        return self.name

推荐答案

使用

或者您甚至可以传递Jinja2宏(该宏必须存在于覆盖模板中):

Or you can even pass a Jinja2 macro (which has to be present in the overriding template):

class CallView(sqla.ModelView):
    column_formatters = dict(path=macro('render_path_mp3'))

在模板中:

{% macro render_path_mp3(model, column) %}
   <a href="{{ url_for('path_to_mp3_view', filename=model.path) }}">{{ model.name }}</a>
{% endmacro %}

创建自定义视图功能 path_to_mp3_view 作为练习已被省略..;)

Creating the custom view function path_to_mp3_view has been left out as an exercise.. ;)

这篇关于使用Flask-Admin的自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆