fix: fix the pagination

Alpha
吴宇涵 2023-03-23 13:47:53 +08:00
parent bdda754af6
commit ec6a2249ae
2 changed files with 7 additions and 11 deletions

View File

@ -25,14 +25,10 @@ def admin():
article_number = get_number_of_articles() article_number = get_number_of_articles()
try: try:
_page_size = min(int(request.args.get("size", 5)), article_number) _page_size = min(max(1, int(request.args.get("size", 5))), article_number) # 最小的size是1
if _page_size <= 0: _cur_page = min(max(1, int(request.args.get("page", 1))), article_number // _page_size + 1) # 最小的page是1
raise ZeroDivisionError
_cur_page = min(int(request.args.get("page", 1)), article_number // _page_size)
except ValueError: except ValueError:
return "page parmas must be int!" return "page parmas must be int!"
except ZeroDivisionError:
return "page size must bigger than zero"
context = { context = {
"article_number": article_number, "article_number": article_number,

View File

@ -101,17 +101,17 @@
</div> </div>
<div style="margin:20px 0;"> <div style="margin:20px 0;">
<ul class="pagination pagination-sm justify-content-center"> <ul class="pagination pagination-sm justify-content-center">
<li class="page-item"><a class="page-link" href="/admin?page={{cur_page-1}}&size={{page_size}}">Previous</a> <li class="page-item"><a class="page-link" href="/admin?page={{ cur_page - 1 }}&size={{ page_size }}">Previous</a>
</li> </li>
{% for i in range(1, article_number//page_size+1) %} {% for i in range(1, article_number // page_size + 2) %}
{% if cur_page == i %} {% if cur_page == i %}
<li class="page-item active"><a class="page-link" href="/admin?page={{i}}&size={{page_size}}">{{ i }}</a> <li class="page-item active"><a class="page-link" href="/admin?page={{ i }}&size={{ page_size }}">{{ i }}</a>
</li> </li>
{% else %} {% else %}
<li class="page-item"><a class="page-link" href="/admin?page={{i}}&size={{page_size}}">{{ i }}</a></li> <li class="page-item"><a class="page-link" href="/admin?page={{ i }}&size={{ page_size }}">{{ i }}</a></li>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
<li class="page-item"><a class="page-link" href="/admin?page={{cur_page+1}}&size={{page_size}}">Next</a> <li class="page-item"><a class="page-link" href="/admin?page={{ cur_page + 1 }}&size={{ page_size }}">Next</a>
</li> </li>
</ul> </ul>
</div> </div>