# 过滤补充; # 点击userprofile表报错; # 因为当没有自定义admin_class时,默认使用的是BaseKingAdmin; # 所以,BaseKingAdmin需要做一些默认的配置; # admin_base.py class BaseKingAdmin(object): list_display = [] list_filter = [] search_fields = [] # 此时列表为空,build_table_row()的将没有返回值,前端显示空; # 做一个判断: # kingadmin_tag.py def build_table_row(obj,admin_class): """生成一条记录的html element""" ele = "" if admin_class.list_display: for column_name in admin_class.list_display: column_obj = admin_class.model._meta.get_field(column_name) if column_obj.choices: #get_xxx_display 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 else: td_ele = "<td>%s</td>" % obj ele += td_ele return mark_safe(ele) # 问题:没有注册admin_class,但是还是显示过滤; # table_obj_list.html <div> <!-- 判断是否有list_filter --> {% if admin_class.list_filter %} <form action=""> {% for filter_column in admin_class.list_filter %} {% build_filter_ele filter_column admin_class %} {% endfor %} <input type="submit" value="过滤"> </form> {% endif %} </div> # django-admin中如果没有注册不显示字段,表头显示表名; # table_obj_list.html <tr> {% if admin_class.list_display %} {% for column in admin_class.list_display %} <th>{{ column }}</th> {% endfor %} {% else %} <th>{{ admin_class.model._meta.model_name }}</th> {% endif %} </tr> # 报错,因为模板语言不允许前缀下划线_meta # 写一个simple_tag # kingadmin_tag.py @register.simple_tag def get_model_name(admin_class): return admin_class.model._meta.model_name.upper() # table_obj_list.html <th>{{ get_model_name admin_class }}</th> ##############################################################
# 分页功能开发; # 处理一:django-pure-pagination # veiws.py @login_required def table_obj_list(request,app_name,model_name): ... # 分页 try: page = request.GET.get(‘page‘, 1) # 出现异常也取第一页 except PageNotAnInteger: page = 1 # 实例化分页器类,5条数据一页 p = Paginator(querysets, 2, request=request) # 通过Paginator内部的page()方法,获取Page分页对象 # 传入当前页码(指定页面) current_page = p.page(page) return render(request,‘kingadmin/table_obj_list.html‘, { ‘querysets‘:querysets, ‘admin_class‘:admin_class, ‘current_page‘:current_page }) # table_obj_list.html <nav aria-label="Page navigation"> <!-- 分页--> <ul class="pagination"> <!-- 上一页 --> <!-- 如果有前一页就显示,没有就不显示,或者其他操作--> {% if current_page.has_previous %} <li class="long"><a href="?{{ current_page.previous_page_number.querystring}}">上一页</a></li> {% else %} {% endif %} <!-- 遍历显示所有页码 --> {% for page in current_page.pages %} <!-- 如果页码存在,就执行 --> {% if page %} <!-- 如果遍历到的页码是当前页,就激活样式 --> {% ifequal page current_page.number%} <li class="active"><a href="?{{ page.querystring }}">{{ page }}</a></li> {% else %} <li><a href="?{{ page.querystring }}">{{ page }}</a></li> {% endifequal %} {% else %} <li class="none"><a href="">...</a></li> {% endif %} {% endfor%} <!-- 下一页 --> {% if current_page.has_next %} <li class="long"><a href="?{{ current_page.next_page_number.querystring}}">下一页</a></li> {% else %} {% endif %} </ul> </nav> # 问题:分页之后再点击过滤,分页不作为条件; # 过滤之后分页,在原url基础之上添加?page=,将过滤后的数据传递给分页; # views.py def get_filter_result(request,querysets): filter_conditions = {} for key,val in request.GET.items(): # 排除分页的影响 if key != ‘page‘: if val: filter_conditions[key] = val else: pass return querysets.filter(**filter_conditions),filter_conditions ###################################################################
原文地址:https://www.cnblogs.com/pymkl/p/8309807.html
时间: 2024-10-13 00:35:21