做了一个带有查询表单和分页功能的页面,希望能正常实现查询信息和分页查询的功能
@app.route('/user', methods=['GET', 'POST'])
@login_required
def user():
user_search_form = UserSearchForm()
page = request.args.get('page', 1, type=int)
search_content = request.args.get('search_content')
if search_content:
user_search_form.search_content.data = search_content
users = User.query.filter(User.username.like('%' + search_content + '%')).paginate(
page=page,
per_page=app.config[
'USERS_PER_PAGE'],
error_out=False)
else:
users = User.query.paginate(page=page,
per_page=app.config[
'USERS_PER_PAGE'],
error_out=False)
if user_search_form.validate_on_submit() and user_search_form.search_submit.data:
users = User.query.filter(User.username.like('%' + user_search_form.search_content.data + '%')).paginate(
page=page,
per_page=app.config[
'USERS_PER_PAGE'],
error_out=False)
next_url = url_for('user',
page=users.next_num,
search_content=user_search_form.search_content.data) if users.has_next else None
prev_url = url_for('user',
page=users.prev_num,
search_content=user_search_form.search_content.data) if users.has_prev else None
return render_template('user.html', users=users.items, next_url=next_url, prev_url=prev_url,
user_search_form=user_search_form)
```”
```html
{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block app_content %}
<h1>你好,{{ current_user.username }}!</h1>
<div class="row">
<div class="form-inline">
{{ wtf.quick_form(user_search_form) }}
</div>
</div>
<br>
<div style="width:1200px;overflow-x:auto">
<table class="table table-hover">
<tr>
<th>姓名</th>
<th>身份证号码</th>
<th>类别</th>
</tr>
{% for user in users %}
<tr>
<td>{{user.username}}</td>
<td>{{user.id_card}}</td>
<td>{{user.organization}}</td>
<td><a href="{{ url_for('alter_user',old_user_id = user.id) }}">修改</a></td>
</tr>
{% endfor%}
</table>
<br>
<p>新用户? <a href="{{ url_for('register') }}">点击添加</a></p>
</div>
<nav aria-label="...">
<ul class="pager">
<li class="previous{% if not prev_url %} disabled{% endif %}">
<a href="{{ prev_url or '#' }}">
<span aria-hidden="true">←</span> 上一页
</a>
</li>
<li class="next{% if not next_url %} disabled{% endif %}">
<a href="{{ next_url or '#' }}">
下一页 <span aria-hidden="true">→</span>
</a>
</li>
</ul>
</nav>
{% endblock %}
在查询表单框输入查询信息后查询,如果点击了下一页,之后再在表单中修改查询内容并点击提交时表单内容不会发生改变。
如在表单中输入“陈”点击查询,之后点击下一页,再在表单中输入“王”点击查询,页面刷新后表单内的信息还是“陈
我个人觉得是user_search_form.search_content.data = search_content 的赋值方式出了问题,不能用data,但是在网上也没有查到相关信息
希望能实现在查询后点击下一页。再输入查询信息时
1.能查询出正确结果
2.在表单显示正确信息