Chapter 5, paging function development
5.1. Modify BaseKingAdmin and improve the previous page display
Now accessing an unregistered model will report an error because list_display and list_filter are not written in the base class.
Just set an empty list in the base class
# kingadmin / admin_base.py class BaseKingAdmin(object): list_display = [] list_filter = []
search_fields = []
Now there is no error in the access, but no data is displayed, you need to add a judgment in build_tab_row
kingadmin_tags.py
@register.simple_tag def build_table_row(obj,admin_class): ''' Generate a record html element ''' ele = '' if admin_class.list_display: for column_name in admin_class.list_display: #Get all field objects column_obj = admin_class.model._meta.get_field(column_name) #choices method of field objects, if there are 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 he += td_ele else : td_ele = "<td>%s</td>"%obj he += all_he
Add a judgment in table_obj_list.html
(2) Improve front-end page display
Configured list_display to display all column names, not configured should display model name
table_obj_lsit.html
<thead <tr> {% if admin_class.list_display %} {% for column in admin_class.list_display %} <th>{{ column }}</th> {% endfor %} {% else %} <th>{% get_model_name admin_class %}</th> {% endif %} </tr> </thead>
kingadmin_tags.py
@register.simple_tag def get_model_name(admin_class): '''获取表名''' return admin_class.model._meta.model_name.upper()
Front-end display effect
5.2. Paging function development
Django official website paginationg instructions
Official website example
1)kingadmin/views.py
@login_required def table_obj_list(request, app_name, model_name): ''' Take out the data in the specified model and return it to the front end ''' #After getting the admin_class, find the model through it 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 #分页 paginator = Paginator(querysets, 2) page = request.GET.get('page') try: querysets = paginator.page(page) except PageNotAnInteger: querysets = paginator.page(1) except EmptyPage: querysets = paginator.page(paginator.num_pages) return render(request, 'kingadmin/table_obj_list.html',{'querysets':querysets,'admin_class':admin_class})
(2)table_obj_lsit.html
<div class="pagination"> <span class="step-links"> {% if querysets.has_previous %} <a href="?page={{ querysets.previous_page_number }}">previous</a> {% endif %} <span class="current"> Page {{ querysets.number }} of {{ querysets.paginator.num_pages }}. </span> {% if querysets.has_next %} <a href="?page={{ querysets.next_page_number }}">next</a> {% endif %} </span> </div>
At this time, when accessing the page, paging is indeed achieved, but clicking the next page will report an error
Because page='2' is used as a filter condition in the background, add a judgment
kingadmin/views.py
5.3. Paging function optimization
Bootstrap pagination component
(1) kingadmin_tag.py
@register.simple_tag def render_paginator(querysets): '''分页''' ele = ''' <ul class="pagination"> ''' # page_range is all pages, querysets.number is the current page for i in querysets.paginator.page_range: #Display three pages before and after, abs is the absolute value if abs(querysets.number - i) < 3 : active = '' if querysets.number == i: #如果是当前页,class='active' active = 'active' p_ele = '''<li class="%s"><a href="?page=%s">%s</a></li>'''%(active,i,i) he += p_he ele += "</ul>" return mark_safe(ele)
(2)table_obj_list.html
Effect:
The code has been synchronized 'num5 to improve the front page display and paging function development'