【问题标题】:Reducing size of columns in Flask-Admin减少 Flask-Admin 中的列大小
【发布时间】:2016-12-10 04:55:39
【问题描述】:

有没有办法限制 ModelView 列的大小(长度/宽度)?我正在使用 WYSIWYG 编辑器,这会创建很长的文本,因此使 ModelView 的列很长。

这是它的外观图片。在右侧查看最后一列。它甚至比屏幕截图所能处理的还要长。

【问题讨论】:

    标签: python twitter-bootstrap flask flask-sqlalchemy flask-admin


    【解决方案1】:

    不显示列(通过排除):

    class MyView(ModelView):
    
        column_exclude_list = ('description')
    

    不显示列(通过包含):

    class MyView(ModelView):
    
        column_list = ('rating', 'category_id', 'year', 'stock', 'image')   
    

    重新格式化列:

    class MyView(ModelView):
    
         def _description_formatter(view, context, model, name):
            # Format your string here e.g show first 20 characters
            # can return any valid HTML e.g. a link to another view to show the detail or a popup window
            return model.description[:20]
    
        column_formatters = {
            'description': _description_formatter,
        }
    

    【讨论】:

    • 在切片之前无需检查边界,因为切片索引由 Python 优雅地处理。第 6 行可以缩短为return model.description[:20]
    【解决方案2】:

    执行此操作的一种方法是覆盖相关列的 css 样式。在 Flask-admin list.html 模板中,您可以找到以下用于创建列的代码:

    {% for c, name in list_columns %}
    <td class="col-{{c}}">
      {% if admin_view.is_editable(c) %}
        {% set form = list_forms[get_pk_value(row)] %}
        {% if form.csrf_token %}
          {{ form[c](pk=get_pk_value(row), display_value=get_value(row, c), csrf=form.csrf_token._value()) }}
        {% else %}
          {{ form[c](pk=get_pk_value(row), display_value=get_value(row, c)) }}
        {% endif %}
      {% else %}
        {{ get_value(row, c) }}
      {% endif %}
    </td>
    {% endfor %}
    

    例如对于第 2 列,您可以将 max-width 属性添加到 css 类 col-2 以限制其宽度。

    【讨论】:

      猜你喜欢
      • 2014-11-28
      • 1970-01-01
      • 2020-03-04
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 2017-12-27
      • 2017-08-21
      • 2016-04-25
      相关资源
      最近更新 更多