kingadmin设计开发
4.4.根据list_display配置生成数据列表
(1)kingadmin/views.py
(2)kingadmin/templates/kingadmin/table_obj_list.html
通过自定义的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 %}
(3)自定义模板标签
kingadmin下创建python package "templatetags",再新建kingadmin_tags.py
# 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)
(4)table_obj_list.html
首先要导入:{% load kingadmin_tags%}
然后使用自定义模板标签:<tr>{% build_table_row obj admin_class %}</tr>
使用的时候需要传两个参数进去
{#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 %}
<tr>{% build_table_row obj admin_class %}</tr>
{% endfor %} </tbody>
</table>
</div> {% endblock %}
现在访问就可以看到对应列的数据了,但是status显示的是“0”,“1”,我们想要的是“未报名”,“已报名”,还有source,contact_tapy
(5)修改kingadmin_tags.py
因为有的字段有choices,有的字段没有choices,需要添加一个判断
models.CustomerInfo._meta.fields 获取model所有字段的对象
models.CustomerInfo._meta.get_field('status') 取一个字段的对象
get_xxxx_display 显示choices里面的值
# 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:
#获取所有字段对象
column_obj = admin_class.model._meta.get_field(column_name)
#字段对象的choices方法,如果有choices,则get_xxx_display
if column_obj.choices:
column_data = getattr(obj,'get_%s_display'%column_name)()
else:
column_data = getattr(obj,column_name) td_ele = "<td>%s</td>"%column_data
ele += td_ele return mark_safe(ele)
现在访问页面
4.5.添加过滤功能
(1)kingadmin/views.py
# kingadmin/templatetags/kingadmin_tags.py
import datetime from django.template import Library
from django.utils.safestring import mark_safe register = Library() @register.simple_tag
def build_filter_ele(filter_column,admin_class):
filter_ele = "<select name='%s'>"%filter_column
column_obj = admin_class.model._meta.get_field(filter_column)
try:
for choice in column_obj.get_choices():
option = "<option value='%s'>%s</option>"%choice
filter_ele += option except AttributeError as e:
#get_internal_type():获取字段属性
#因为时间的过滤方式是固定的(今天,过去七天,一个月.....),而不是从后台获取的
if column_obj.get_internal_type() in ('DateField','DateTimeField'):
time_obj = datetime.datetime.now()
time_list = [
['','--------'],
[time_obj,'Today'],
[time_obj - datetime.timedelta(7),'七天内'],
[time_obj.replace(day=1),'本月'],
[time_obj - datetime.timedelta(90),'三个月内'],
[time_obj.replace(month=1,day=1),'YearToDay(YTD)'], #本年
['','ALL'],
] for i in time_list:
option = "<option value='%s'>%s</option>" %(i[0],i[1])
filter_ele += option filter_ele += "</select>" return mark_safe(filter_ele)
(2)table_obj_list.html
<div>
<form>
{% for filter_column in admin_class.list_filter %}
{% build_filter_ele filter_column admin_class %}
{% endfor %}
<input type="submit" value="过滤">
</form>
</div>
访问页面
接下来把数据返回到前端
(3)kingadmin/views.py
def get_filter_result(request,querysets):
filter_conditions = {}
#获取过滤的字段
for key,val in request.GET.items():
if val:
filter_conditions[key] = val
#返回过滤后的数据
return querysets.filter(**filter_conditions),filter_conditions @login_required
def table_obj_list(request, app_name, model_name):
'''取出指定model里的数据返回给前端'''
#拿到admin_class后,通过它找到拿到model
admin_class = site.enable_admins[app_name][model_name]
querysets = admin_class.model.objects.all()
querysets,filter_conditions = get_filter_result(request,querysets)
admin_class.filter_conditions = filter_conditions
return render(request, 'kingadmin/table_obj_list.html',{'querysets':querysets,'admin_class':admin_class})
(4)kingadmin_tags.py
当选择过滤条件后,给对应值添加选中状态 selected=selected
现在通过时间过滤会报格式不正确的错误
修改时间过滤的格式
现在不报错了,但并没有过滤出数据
因为其它条件过滤的时候值是等于,但是根据时间过滤应该是大于等于才对
修改kingadmin_tags.py