分页显示的制作流程


1.在app下新建名为templatetags的python包,并在其中新建mytags.py文件(此文件用于书写函数)

2.在新建的mytags.py文件中写入以下两行代码:
  from django import template
  register=template.Library()

  然后定义函数:
  def pagination(total,current)
    return {'total':total,'current':current}

  函数定义上方写装饰器:@register.inclusion_tag('pagination.html')

3.书写pagination.html模板
  将母版中的分页部分取出来,修改
  {% for num in total %}
  {% if num == current %}
  <li class="active"><a href="#">{{ num }}</a></li>
  {% else %}
  <li><a href="#">{{ num }}</a></li>
  {% endif %}
  {% endfor %}
4.使用
  {% load mytags %}
  {% 函数名 参数1 参数2 %}

原文地址:https://www.cnblogs.com/wdbgqq/p/9699851.html