You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Django中创建通用HTML表格模板以复用展示多实体?

Django通用HTML表格实现方案

一、修正当前模板的变量取值问题

你当前模板里的row.{{ header_name }}写法不符合Django模板语法规则,模板不支持嵌套变量解析。针对不同的数据类型,有两种解决方式:

1. 数据是Django模型实例(比如从数据库查询的对象)

使用getattr模板过滤器来动态获取实例属性:

<table class="table table-bordered" id="vendor_table" style="text-align: center;">
    <thead class="tables-success"> 
        <tr>
            {% for header in list_headers %}
                <th>{{ header }}</th>
            {% endfor %}
        </tr>    
    </thead>
    <tbody>
        {% for row in list_values %}
        <tr>
            {% for header_name in list_headers %}
                <td> {{ row|getattr:header_name }} </td>
            {% endfor %}
        </tr>  
        {% endfor %} 
    </tbody>
</table>

2. 数据是字典类型

直接通过键取值即可:

<table class="table table-bordered" id="vendor_table" style="text-align: center;">
    <thead class="tables-success"> 
        <tr>
            {% for header in list_headers %}
                <th>{{ header }}</th>
            {% endfor %}
        </tr>    
    </thead>
    <tbody>
        {% for row in list_values %}
        <tr>
            {% for header_name in list_headers %}
                <td> {{ row.header_name }} </td>
            {% endfor %}
        </tr>  
        {% endfor %} 
    </tbody>
</table>

二、更通用的模板复用方案:封装为模板标签(Inclusion Tag)

如果要在多个页面复用通用表格,推荐将表格逻辑封装成包含型模板标签,可以自定义参数(比如表格ID、样式类、操作列等),灵活性更高。

1. 创建模板标签文件

在你的Django应用下新建templatetags目录(如果没有的话),并创建table_tags.py文件:

from django import template

register = template.Library()

@register.inclusion_tag('common/table.html')
def render_generic_table(list_headers, list_values, table_id='', table_class='table table-bordered', text_align='center'):
    """
    渲染通用HTML表格
    :param list_headers: 表头列表,比如['名称', '联系人', '电话']
    :param list_values: 数据列表,每个元素是模型实例或字典
    :param table_id: 表格ID
    :param table_class: 表格样式类
    :param text_align: 文本对齐方式
    """
    return {
        'list_headers': list_headers,
        'list_values': list_values,
        'table_id': table_id,
        'table_class': table_class,
        'text_align': text_align,
    }

2. 创建通用表格模板

在templates/common/目录下新建table.html:

<table class="{{ table_class }}" id="{{ table_id }}" style="text-align: {{ text_align }};">
    <thead class="tables-success"> 
        <tr>
            {% for header in list_headers %}
                <th>{{ header }}</th>
            {% endfor %}
        </tr>    
    </thead>
    <tbody>
        {% for row in list_values %}
        <tr>
            {% for header_name in list_headers %}
                <td> {{ row|getattr:header_name }} </td>
            {% endfor %}
        </tr>  
        {% endfor %} 
        {% if not list_values %}
        <tr>
            <td colspan="{{ list_headers|length }}">暂无数据</td>
        </tr>
        {% endif %}
    </tbody>
</table>

3. 在页面中调用模板标签

在需要展示表格的模板里,先加载标签库,再调用:

{% load table_tags %}

{% render_generic_table list_headers list_values table_id='vendor_table' %}

三、额外优化建议

  • 字段格式化:如果需要对特定字段(比如日期、布尔值)做格式化,可以在模板标签函数中提前处理数据,或者自定义过滤器来实现。
  • 操作列支持:如果需要添加编辑、删除等操作按钮,可以在render_generic_table函数中增加actions参数,在模板中动态渲染操作列。

内容的提问来源于stack exchange,提问作者Diego Quirós

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 19:22:27