Flask wtforms AttributeError:“ HTMLString”对象没有属性“ paginate”

时间:2019-11-19 09:56:51

标签: flask pagination flask-sqlalchemy flask-wtforms

有人在这里提供了一些很棒的代码来帮助我,每次都使用一个提交按钮多次显示相同的表单,这很有效,但是由于我将需要数百种表单来对页面进行分页,因此我能够在过去分页页面,但是我不知道如何在for循环中将代码与表单一起使用。 这是我的代码:(在Greg的帮助下)

@bp.route('/stock', methods=['GET', 'POST'])
@bp.route('/stock/stock/', methods=['GET', 'POST'])
@login_required
def stock():
    stocks = Stock.query.all()
    forms = []
    for stock in stocks:
        form = AddStockForm()
        form.id.default = stock.id
        form.image.default = stock.image_url
        form.date.default = stock.date
        form.description.default = stock.description
        form.event.default = stock.event
        form.achat.default = stock.achat
        form.vente.default = stock.vente
        form.sold.default = stock.sold
        forms.append(form)

    for form in forms:
        if form.validate_on_submit():
            if form.modify.data:
                stock = Stock.query.filter_by(id=form.id.data).one()
                stock.date = form.date.data
                stock.description = form.description.data
                stock.event = form.event.data
                stock.achat = form.achat.data
                stock.vente = form.vente.data
                stock.sold = form.sold.data
                db.session.add(stock)
                db.session.commit()
            elif form.delete.data:
                stock = Stock.query.filter_by(id=form.id.data).one()
                db.session.delete(stock)
                db.session.commit()
            return redirect(url_for('stock.stock'))

        form.process()  # Do this after validate_on_submit or breaks CSRF token
        page = request.args.get('page', 1, type=int)
        forms = forms[1].id().paginate(
             page, current_app.config['ITEMS_PER_PAGE'], False)
        next_url = url_for('stock.stock', page=forms.next_num) \
            if forms.has_next else None
        prev_url = url_for('stock.stock', page=forms.prev_num) \
            if forms.has_prev else None
    return render_template('stock/stock.html',forms=forms.items, title=Stock, stocks=stocks)

我正在尝试使用事实“ forms”是一个列表来对结果进行分页,我显然不明白该怎么做,我看过flask-paginate,但我也不明白! 急需所有帮助 保罗,致以亲切的问候。

编辑 我尝试使用flask_pagination,这是我的代码:

@bp.route('/stock/stock/', methods=['GET', 'POST'])
@login_required
def stock():
    search = False
    q = request.args.get('q')
    if q:
        search = True
    page = request.args.get(get_page_parameter(), type=int, default=1)
    stocks = Stock.query.all()
    forms = []
  #rest of code here#
        pagination = Pagination(page=page, total=stocks.count(), search=search, record_name='forms')
        form.process()  # Do this after validate_on_submit or breaks CSRF token
   return render_template('stock/stock.html',forms=forms, title=Stock, pagination=pagination)

这给出了一个不同的错误“ TypeError:count()恰好接受一个参数(给定0)”,我也尝试了“ total = forms.count()”并得到了相同的错误!

1 个答案:

答案 0 :(得分:0)

我讨厌这样做,因为它在一开始就表现出缺乏耐心,但是这个答案可能对其他人有帮助。我以两种方式解决了我的问题,第一种是查询,该查询确定显示的顺序(降序或升序),然后允许我使用flask-paginate在几页上显示结果,我意识到我正在处理一个列表,并且一名开发人员link的示例向我展示了方法,这是我的代码,

from flask_paginate import Pagination, get_page_args


@bp.route('/stock', methods=['GET', 'POST'])
@bp.route('/stock/stock/', methods=['GET', 'POST'])
@login_required
def stock():
    stocks = Stock.query.order_by(Stock.id.desc())# this gives order of results
    forms = []
    def get_forms(offset=0, per_page=25): #this function sets up the number of
        return forms[offset: offset + per_page]  #results per page
for stock in stocks:
    form = AddStockForm()
    form.id.default = stock.id
    form.image.default = stock.image_url
    form.date.default = stock.date
    form.description.default = stock.description
    form.event.default = stock.event
    form.achat.default = stock.achat
    form.vente.default = stock.vente
    form.sold.default = stock.sold
    forms.append(form)
for form in forms:
    if form.validate_on_submit():
        if form.modify.data:
            stock = Stock.query.filter_by(id=form.id.data).one()
            stock.date = form.date.data
            stock.description = form.description.data
            stock.event = form.event.data
            stock.achat = form.achat.data
            stock.vente = form.vente.data
            stock.sold = form.sold.data
            db.session.add(stock)
            db.session.commit()
        elif form.delete.data:
            stock = Stock.query.filter_by(id=form.id.data).one()
            db.session.delete(stock)
            db.session.commit()
        return redirect(url_for('stock.stock'))

    #this is the code from the link that I used to paginate
    page, per_page, offset = get_page_args(page_parameter='page',
                                           per_page_parameter='per_page')
    total = len(forms) # this code counts the resulting list to be displayed
    pagination_forms = get_forms(offset=offset, per_page=per_page)
    pagination = Pagination(page=page, per_page=per_page, total=total)
    form.process()  # Do this after validate_on_submit or breaks CSRF token

return render_template('stock/stock.html', title=Stock, stocks=stocks
                       page=page, forms=pagination_forms, per_page=per_page, pagination=pagination)
#And finally this is the pagination passed to the html

因此,对于像我一样努力与一切奋斗但仍然喜欢它的所有脾气暴躁的人来说。