我正在尝试使用Flask-SQLAlchemy实现分页
这是我为路线所做的
@app.route('/client/<int:page_num>',methods=['POST','GET'])
def client(page_num=None):
form = ClientForm(request.form)
if current_user.is_active ==True:
if current_user.user_role == "super_admin":
if form.validate_on_submit():
message=addOrUpdateClient(form, current_user)
return jsonify(json.dumps(message))
if (page_num):
clients = Client.query.paginate(per_page=5, page=page_num, error_out=True)
else:
clients = Client.query.paginate(per_page=5, page=1, error_out=True)
return render_template('client.html/', user=current_user,form = form,clients= clients)
else:
next_page = url_for('login')
return redirect(next_page)
我的模板文件是
{% if clients %}
<h4>All client</h4>
<div class="table-responsive">
<table class="table table-striped" id="client_table">
<thead>
<tr>
<th>Client Name</th>
<th>Client Short Name</th>
</tr>
</thead>
<tbody>
{% for client in clients.items %}
<tr>
<td>{{client.client_name}}</td>
<td>{{client.client_code}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% for client in clients.iter_pages(left_edge=3, right_edge=3, left_current=3, right_current=3) %}
{% if page %}
<a href="{{ url_for('client',page_num=page) }}">{{page}}</a>
{% else %}
{% endif %}
{% endfor %}
{% endif %}
但是当我尝试加载时,出现以下错误
Could not build url for endpoint 'client'. Did you forget to specify values ['page_num']?
在添加此分页之前,我的代码工作正常。这里还要指出的一件事是,我什至没有打过'/ client'路线。我在索引页http://localhost:5000/
修改:
看来,虽然我仍然不确定
,但是我的html模板中有问题我在html模板中构建用于导航的网址,例如
<li class="nav-item">
<a class="nav-link" href="{{ url_for('client') }}">
<i class="ti-shield menu-icon"></i>
<span class="menu-title">Client</span>
</a>
</li>
IDE在此行指向错误。