我在页面上有一个循环的jinja块,我想对它进行分页。我正在关注this教程,但得到“ TypeError:count()恰好接受一个参数(给定0)。”我认为问题出在传递home_data信息,但是我不知道该怎么做。这是我的.py:
from flask import Blueprint
from flask_paginate import Pagination, get_page_parameter
mod = Blueprint('home_data', __name__)
@app.route('/')
def home():
search = False
q = request.args.get('q')
if q:
search = True
page = request.args.get(get_page_parameter(), type=int, default=1)
home_data = Article.query.all()
pagination = Pagination(page=page, total=home_data.count(), search=search, record_name='home_data')
return render_template('home.html', home_data=Article.query.all(), pagination=pagination,)
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
{{ pagination.info }}
{{ pagination.links }}
{% for dict_item in home_data|reverse %}
<!--<a href="results/{{ dict_item['id'] }}">-->
<div class="photo">
<img src="{{ dict_item['image_url'] }}" width="100%">
</div>
<div class="hometitlesstyle">{{ dict_item['title'] }}</div>
<div class="voteorresult"><a href="votefor/{{ dict_item['id'] }}">Vote</a></div>
<div class="resultbox"><a href="results/{{ dict_item['id'] }}">Results</a></div>
<hr class="new1">
{% endfor %}
{{ pagination.links }}
</body>
</html>
答案 0 :(得分:1)
在home_data = Article.query.all()
中,all()
将查询转换为python列表。使用home_data = Article.query
将其保留为查询。这样,count()
就可以在查询上工作,而不是在列表上工作。
替代使用
home_data = Article.query.all()
pagination = Pagination(page=page, total=len(home_data), search=search, record_name='home_data')