这就是我所做的。首先,您需要限制您的数据库请求,以便如果您的结果是 2,000 条记录,您只想查看前 50 条记录,您可以在查询结束时执行类似的操作....
"order by TableName.SomeID limit " + str(int(CurrentPage) * 50 - 50) + ", 50;"
以下是有关限制和偏移量的更多信息:http ://www.mysqltutorial.org/mysql-limit.aspx
无论如何,使用 CurrentPage 来获取您的偏移量,以便在第 1 页上限制看起来像这个限制 0、50 - 意思是给我 50 行并从第 0 行开始。在第 2 页上,限制看起来像这个限制 50、50 - 意思给我 50 行并从第 50 行开始。此外,您需要在其中保留 order by 子句,以便在分页时结果保持一致的顺序。
您还需要无限制地运行另一个查询,以便知道总共有多少页。此查询只需选择 count(*) 即可。这是我从该查询中计算总页数的方法...
# 50 for the number of results per page
TotalPages = (ResultsCount[0]['Count(*)'] // 50)
# find out if there is a remainder of results if so add one more page
if((ResultsCount[0]['Count(*)'] % 50) > 0):
TotalPages +=1
现在你的 jinja2 应该看起来像这样......
{% if TotalPages > 1 and CurrentPage < 15 %}
{% for Item in range(TotalPages) %}
{% if loop.index == CurrentPage %}
<div style="border: 1px solid; border-color: #1C5F8B; width: 22px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px"><b>{{ loop.index }}</b></div>
{% elif loop.index < 15 %}
<a style="cursor: pointer; color: #000000" href="/class/getdata?CurrentPage={{ loop.index }}"><div style="border: 1px solid; width: 22px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px">{{ loop.index }}</div></a>
{% elif loop.index == 15 %}
<a style="cursor: pointer; color: #000000" href="/class/getdata?CurrentPage={{ loop.index }}"><div style="border: 1px solid; width: 28px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px">Next</div></a>
{% endif %}
{% endfor %}
{% elif TotalPages > 1 and (CurrentPage + 15) < TotalPages %}
{% for Item in range(15 * CurrentPage) %}
{% if loop.index0 == (CurrentPage - 15) %}
<a style="cursor: pointer; color: #000000" href="/class/getdata?CurrentPage={{ loop.index }}"><div style="border: 1px solid; width: 28px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px">Prev</div></a>
{% elif loop.index == CurrentPage %}
<div style="border: 1px solid; border-color: #1C5F8B; width: 22px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px"><b>{{ loop.index }}</b></div>
{% elif loop.index > CurrentPage and loop.index < (CurrentPage + 15) and loop.index < TotalPages %}
<a style="cursor: pointer; color: #000000" href="/class/getdata?CurrentPage={{ loop.index }}"><div style="border: 1px solid; width: 22px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px">{{ loop.index }}</div></a>
{% elif loop.index == (CurrentPage + 15) and loop.index < TotalPages %}
<a style="cursor: pointer; color: #000000" href="/class/getdata?CurrentPage={{ loop.index }}"><div style="border: 1px solid; width: 28px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px">Next</div></a>
{% elif loop.index == TotalPages and (CurrentPage + 15) > TotalPages %}
<a style="cursor: pointer; color: #000000" href="/class/getdata?CurrentPage={{ loop.index }}"><div style="border: 1px solid; width: 22px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px">{{ loop.index }}</div></a>
{% endif %}
{% endfor %}
{% elif TotalPages > 1 %}
{% for Item in range(15 * CurrentPage) %}
{% if (loop.index - 1) == (CurrentPage - 15) %}
<a style="cursor: pointer; color: #000000" href="/class/getdata?CurrentPage={{ loop.index }}"><div style="border: 1px solid; width: 28px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px">Prev</div></a>
{% elif loop.index == CurrentPage %}
<div style="border: 1px solid; border-color: #1C5F8B; width: 22px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px"><b>{{ loop.index }}</b></div>
{% elif loop.index > (TotalPages - 15) and loop.index < (CurrentPage + 15) and loop.index < TotalPages %}
<a style="cursor: pointer; color: #000000" href="/class/getdata?CurrentPage={{ loop.index }}"><div style="border: 1px solid; width: 22px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px">{{ loop.index }}</div></a>
{% elif loop.index == TotalPages and (CurrentPage + 15) > TotalPages %}
<a style="cursor: pointer; color: #000000" href="/class/getdata?CurrentPage={{ loop.index }}"><div style="border: 1px solid; width: 22px; height: 16px; text-align: center; float: left; margin: 2px; padding-top: 2px">{{ loop.index }}</div></a>
{% endif %}
{% endfor %}
{% endif %}
希望这可以帮助!