具有分页功能的Flask-SQLAlchemy和GET请求

时间:2018-01-23 18:30:42

标签: python python-3.x pagination flask-sqlalchemy

我正在尝试为简单网站构建分页搜索表单。我的问题是这个 - 当我离开第1页(通过分页),执行或更新搜索,而不是返回到第1页时,表单会重新加载到我当时碰巧在的任何页面上。这会打破新结果集的显示。

例如,如果URL是这个,并且我更新搜索并点击提交,我会留在第二页,而不是从一开始。 ?mysite的/ coorddash / 2 coord_status =待定&安培;起始日期= 01 /2017分之23&安培; END_DATE = 01 /二千〇一十八分之二十三

# VIEWS
@app.route('/coorddash/<int:page>', methods=['GET', 'POST'])
def coordDash(page=1):
    per_page = 3
    existingISG = ISG.query \
    .filter(or_(ISG.coordinator_status == 'IAT Verified', ISG.coordinator_status == 'New')) \
    .order_by("isg.id desc") \
    .paginate(page,per_page,error_out=False)
    form=coordDashForm(request.args)
    coordstatus=start=end=""

    if form.validate():
        coordstatus = request.values.get("coord_status")
        form.coord_status.data = coordstatus
        start = request.values.get("start_date")
        form.start_date.data = datetime.strptime(start, '%m/%d/%Y')
        end = request.values.get("end_date")
        form.end_date.data = datetime.strptime(end, '%m/%d/%Y')

        existingISG = ISG.query.filter(ISG.coordinator_status == coordstatus).filter(ISG.isg_created_on.between(start, end)).paginate(page,per_page,error_out=False)

    return render_template('coorddash.html', 
                        title = 'Coordinator Dashboard', 
                        existingisg = existingISG, 
                        form=form,
                        url_vals=request.query_string.decode())

#MODELS
class ISG(db.Model):
    __tablename__ = 'isg'
    id = db.Column(db.Integer, primary_key=True)
    coordinator_status = db.Column(db.String(30), unique=False)
    startdate = db.Column(db.DateTime)
    enddate = db.Column(db.DateTime)

#HTML
<form action="" method="get" name="searchForm">
    {{ form.coord_status.label }}:{{ form.coord_status }} | {{ form.sort_by.label }}:{{ form.sort_by }}<br>
    {{ render_field(form.start_date) }}-{{ render_field(form.end_date) }}<br>
    {{ form.submit }}
    {{ form.csrf_token }}
</form>

{% for item in existingisg.items %}
<div>
    <span>{{ item.location }}</span>
    <span>{{ item.implementing_team }}</span>
</div>
{% endfor %}

{% if existingisg.has_prev %}<a href="{{ url_for('coordDash', page=existingisg.prev_num) }}{% if url_vals|length > 0 %}{{ url_vals }}{% endif %}"> Previous Page</a>{% else %}No previous{% endif %} | 
{% if existingisg.has_next %}<a href="{{ url_for('coordDash', page=existingisg.next_num) }}{% if url_vals|length > 0 %}{{ url_vals }}{% endif %}">Next Page </a>{% else %}No more{% endif %}

1 个答案:

答案 0 :(得分:0)

It looks like you're passing back the pagination as a URL variable on reload of the page. Even with a new query, mysite.coorddash/2 is being sent to your coordDash method and you get the second page of results instead of the first.

If you change your form action to reference the current URL with a trailing /1 for the pagination, it should be sent to the correct view and pagination should restart. For example,

<form action={{ url_for('coordDash', page=1) }} method="get" name="searchForm">

The syntax may be slightly off, but something like this will send your results to the correct function for that URL and specify pagination.