美文网首页Python Web开发学习
【CRM客户关系管理】20.增加详情页分页功能

【CRM客户关系管理】20.增加详情页分页功能

作者: 吾星喵 | 来源:发表于2018-12-23 15:54 被阅读0次

分页

修改分页(设置默认显示多少条数据)

修改父类默认分页个数

在djadmin应用的djadmin_base.py中增加一个list_per_page属性,当没有在子类中定义该值时,直接使用父类的属性

# djadmin/djadmin_base.py


class BaseDjAdmin(object):
    list_display = []
    list_filter = []
    search_fields = []
    readonly_fields = []
    filter_horizontal = []
    list_per_page = 5

修改详情页获取分页值

修改djadmin应用的views.py

@login_required
def table_detail(request, app_name, model_name):
    """取出指定model里的数据返回到前端"""
    # 拿到admin_class后,通过它获取model
    admin_class = site.enable_admins[app_name][model_name]
    # print(admin_class)  # 执行djadmin.py定义的注册模型类
    queryset = admin_class.model.objects.all().order_by('-id')
    # print(queryset)

    # 进行过滤
    queryset, filter_conditions = get_filter_result(request, queryset)
    # 将过滤字典保存到全局注册类中
    admin_class.filter_conditions = filter_conditions

    # 搜索
    queryset, keyword = get_search_result(request, queryset, admin_class)
    admin_class.search_keyword = keyword  # 将搜索字符串保存到全局类中

    # 排序,返回排序的结果和排序的字段字典
    queryset, current_order_field = get_order_result(request, queryset, admin_class)
    # print(current_order_field)  # {'consult_content': '4'}
    # 如果有排序,保存排序的值,用于模板中在分页模块显示
    if current_order_field.values():
        current_order_value = list(current_order_field.values())[0]
    else:
        current_order_value = ''

    # 查询集结果分页
    # paginator = Paginator(queryset, 10)  # Show 10 contacts per page
    paginator = Paginator(queryset, admin_class.list_per_page)  # 获取djadmin.py中自定义的分页数量
    page = request.GET.get('page')
    try:
        queryset = paginator.get_page(page)
    except PageNotAnInteger:
        queryset = paginator.get_page(1)
    except EmptyPage:
        queryset = paginator.get_page(paginator.num_pages)

    return render(request, 'djadmin/table_detail.html', locals())

相关文章

网友评论

    本文标题:【CRM客户关系管理】20.增加详情页分页功能

    本文链接:https://www.haomeiwen.com/subject/tisnkqtx.html