标签:queryset content The display body etag simple class类 ade
通过自定义的admin_class类,循环出list_display所有的列
{#kingadmin/templates/kingadmin/table_obj_list.html#}
{% extends ‘kingadmin/index.html‘ %}
{% block right-content-container %}
<h2 class="page-header">app</h2>
<div>
{{ querysets }}
<table class="table table-striped">
<thead>
<tr>
{% for column in admin_class.list_display %}
<th>{{ column }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
{% endblock %}
# kingadmin/templatetags/kingadmin_tags.py
from django.template import Library
from django.utils.safestring import mark_safe
register = Library()
@register.simple_tag
def build_table_row(obj,admin_class):
‘‘‘生成一条记录的html element‘‘‘
ele = ‘‘
for column_name in admin_class.list_display:
#通过反射获取列的数据,两个参数,一个是object,一个是列名
column_data = getattr(obj,column_name)
td_ele = "<td>%s</td>"%column_data
ele += td_ele
return mark_safe(ele)
对应的html
{#kingadmin/templates/kingadmin/table_obj_list.html#}
{% extends ‘kingadmin/index.html‘ %}
{% load kingadmin_tags %}
{% block right-content-container %}
<h2 class="page-header">app</h2>
<div>
{{ querysets }} ---->这是返回的数据集,有几条记录就有几个,
<table class="table table-striped">
<thead>
<tr>
{% for column in admin_class.list_display %}
<th>{{ column }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for obj in querysets %} ----->obj就是一个数据,
<tr>{% build_table_row obj admin_class %}</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
标签:queryset content The display body etag simple class类 ade
原文地址:https://www.cnblogs.com/andy0816/p/13471436.html