Python烧瓶分页将表单变量传递给查询

时间:2016-06-01 22:21:03

标签: python flask pagination sqlalchemy

我正在尝试将表单变量传递给分页。如果我硬编码搜索,那么我的分页工作。我的非硬编码查询适用于第1页,但在尝试进入更多页面时,我收到错误:

TypeError: cannot concatenate 'str' and 'NoneType' objects

我只发布了我认为相关的代码。

@search.route('/search', methods=['GET', 'POST'])
def search():
    form = searchForm()
    search_term = form.searchString.data

    print search_term
    search = False
    q = request.args.get('q')
    if q:
        search = True

    try:
        page = int(request.args.get('page', 1))
    except ValueError:
        page = 1

    PER_PAGE = 10
    inner_window = 4
    outer_window = 3
    offset = page * 1


    #doesnt work
    links = Item.query.filter(Item.title.like('%' + search_term + '%')).paginate(page, 10, True)

    #works
    links = Item.query.filter(Item.title.like('%hel%')).paginate(page, 10, True)

    #doesnt work at all
    #links = db.query.filter(func.mid(Item.description, 1, 3).all()).paginate(page, 10, True)

    total = Item.query.filter(Item.title.like('%' +search_term + '%')).count()
    pagination = get_pagination(page=page,
                                links=links,
                                 per_page=PER_PAGE,
                                offset=offset,

                                search_term=search_term,
                                inner_window=inner_window,
                                outer_window = outer_window,
                                search=search,


                                total = total,        #total number of results
                                format_total=True,   #format total. example 1,024
                                format_number=True,  #turn on format flag
                                record_name='links'

                            )

    return render_template('search/searchPage.html', form=form, links=links, pagination=pagination, page=page, per_page = PER_PAGE)

search_term在第1页上正确打印表单输入,但第2页打印"无"

0 个答案:

没有答案