您现在的位置是: 网站首页 >Django >DjangoCRM客户关系管理 Django

【CRM客户关系管理】12.查询结果分页、排序、过滤组合

admin2018年12月4日 09:36 Django | Html | Python 265人已围观

DjangoCRM客户关系管理简介 使用Django2.1.3+Bootstrap实现CRM系统,仿照Django Admin重写后台 Github地址:https://github.com/xyliurui/DjangoCRM Django版本:2.1.3

### 排序和过滤组合 在`table_detail(request, app_name, model_name)`视图中,将筛选字典保存到了全局注册类中`admin_class.filter_conditions = filter_conditions`,只需要知道`admin_class`就可以得到筛选字典 #### 将筛选字典拼接链接 ```python @register.simple_tag def render_filter_args(admin_class): """拼接过滤的字段""" if admin_class.filter_conditions: tmp = '' for k, v in admin_class.filter_conditions.items(): tmp += '&{}={}'.format(k, v) return mark_safe(tmp) return '' ``` #### 模板中链接增加筛选字段 在a标签中排序后面增加`{% render_filter_args admin_class %}`用于拼接筛选字段 ```html {% if admin_class.list_display %} {% for display_field in admin_class.list_display %} <th> <a href="?_order={% get_sorted_data display_field current_order_field forloop.counter0 %}{% render_filter_args admin_class %}"> {{ display_field }} {% get_sorted_arrow display_field current_order_field forloop.counter0 %} </a> </th> {% endfor %} {% else %} {% build_table_head_name admin_class %} {% endif %} ``` ![BLOG_20181204_094359_51](/media/blog/images/2018/12/BLOG_20181204_094359_51.png "博客图集BLOG_20181204_094359_51.png") 进行筛选,然后排序,此时的链接变成 http://127.0.0.1:8000/djadmin/crm/customerinfo/?_order=-5&consultant=2&created_time__gte=2018-11-06 ### 过滤和分页组合 #### 分页模块增加过滤字段 修改djadmin应用下的templates/includes/pagination.html文件,在每个a标签后面增加`{% render_filter_args admin_class %}` ```html {% load djadmin_tags %} <ul class="pagination pagination-sm m-0 float-right"> {% if page.number > 1 %} <li class="page-item"><a class="page-link" href="?page=1{% render_filter_args admin_class %}" aria-label="">首页</a></li> {% endif %} {% if page.has_previous %} <li class="page-item"><a class="page-link" href="?page={{ page.previous_page_number }}{% render_filter_args admin_class %}">&laquo;</a></li> {% endif %} {% for pg in page.paginator.page_range %} {% if page.number == pg %} <li class="page-item active"><a class="page-link" href="?page={{ pg }}{% render_filter_args admin_class %}">{{ pg }}</a></li> {% elif pg > page.number|add:'-3' and pg < page.number|add:'3' %} <li class="page-item"><a class="page-link" href="?page={{ pg }}{% render_filter_args admin_class %}">{{ pg }}</a></li> {% endif %} {% endfor %} {% if page.has_next %} <li class="page-item"><a class="page-link" href="?page={{ page.next_page_number }}{% render_filter_args admin_class %}">&raquo;</a></li> {% endif %} {% if page.number != page.paginator.num_pages %} <li class="page-item"><a class="page-link" href="?page={{ page.paginator.num_pages }}{% render_filter_args admin_class %}" aria-label="">尾页</a></li> {% endif %} </ul> ``` ### 分页、排序、过滤组合 一般是先进行筛选过滤,然后排序,再分页。这样就只需要在分页上加入排序的字段。 #### 修改分页模块增加排序字段 修改pagination.html文件,增加排序的字段`&_order={{ current_order_num }}` ```html {% load djadmin_tags %} <ul class="pagination pagination-sm m-0 float-right"> {% if page.number > 1 %} <li class="page-item"><a class="page-link" href="?page=1&_order={{ current_order_field }}&_order={{ current_order_value }}{% render_filter_args admin_class %}" aria-label="">首页</a></li> {% endif %} {% if page.has_previous %} <li class="page-item"><a class="page-link" href="?page={{ page.previous_page_number }}&_order={{ current_order_value }}{% render_filter_args admin_class %}">&laquo;</a></li> {% endif %} {% for pg in page.paginator.page_range %} {% if page.number == pg %} <li class="page-item active"><a class="page-link" href="?page={{ pg }}&_order={{ current_order_value }}{% render_filter_args admin_class %}">{{ pg }}</a></li> {% elif pg > page.number|add:'-3' and pg < page.number|add:'3' %} <li class="page-item"><a class="page-link" href="?page={{ pg }}&_order={{ current_order_value }}{% render_filter_args admin_class %}">{{ pg }}</a></li> {% endif %} {% endfor %} {% if page.has_next %} <li class="page-item"><a class="page-link" href="?page={{ page.next_page_number }}&_order={{ current_order_value }}{% render_filter_args admin_class %}">&raquo;</a></li> {% endif %} {% if page.number != page.paginator.num_pages %} <li class="page-item"><a class="page-link" href="?page={{ page.paginator.num_pages }}&_order={{ current_order_value }}{% render_filter_args admin_class %}" aria-label="">尾页</a></li> {% endif %} </ul> ``` #### 详情模板包含的分页模块 传递参数`current_order_num=current_order_field.consult_content`, ```html <div> {% include 'includes/pagination.html' with page=queryset current_order_value=current_order_value %} </div> ``` #### 详情视图排序索引 由于`current_order_field`返回的是一个字典,需要获取其中的值 ```python @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() # print(queryset) # 进行过滤 queryset, filter_conditions = get_filter_result(request, queryset) # 将过滤字典保存到全局注册类中 admin_class.filter_conditions = filter_conditions # 排序,返回排序的结果和排序的字段字典 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 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()) ``` ![BLOG_20181204_094324_74](/media/blog/images/2018/12/BLOG_20181204_094324_74.png "博客图集BLOG_20181204_094324_74.png") 此时链接: http://127.0.0.1:8000/djadmin/crm/customerinfo/?source=&consultant=2&status=1&created_time__gte=2018-11-01 ![BLOG_20181204_094315_61](/media/blog/images/2018/12/BLOG_20181204_094315_61.png "博客图集BLOG_20181204_094315_61.png") 此时链接: http://127.0.0.1:8000/djadmin/crm/customerinfo/?_order=-5&consultant=2&status=1&created_time__gte=2018-11-01 ![BLOG_20181204_094301_86](/media/blog/images/2018/12/BLOG_20181204_094301_86.png "博客图集BLOG_20181204_094301_86.png") 此时链接: http://127.0.0.1:8000/djadmin/crm/customerinfo/?page=1&_order=-5&consultant=2&status=1&created_time__gte=2018-11-01

很赞哦! (0)

文章交流

  • emoji
0人参与,0条评论

当前用户

未登录,点击   登录

站点信息

  • 建站时间:网站已运行590天
  • 系统信息:Linux
  • 后台程序:Python: 3.6.6
  • 网站框架:Django: 2.2.9
  • 文章统计:222 篇
  • 文章评论:40 条
  • 腾讯分析网站概况-腾讯分析
  • 百度统计网站概况-百度统计
  • 微信公众号:扫描二维码,关注我们
返回
顶部
标题 换行 登录
网站