Fork me on GitHub

用flask实现的添加后保留原url搜索条件

1、具体实现

#!usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask,render_template,request,redirect
from pager import Pagination
from urllib.parse import urlencode
app = Flask(__name__)

@app.route('/pager')
def pager():
    li = []
    for i in range(1,100):
        li.append(i)
    # print(li)
    pager_obj = Pagination(request.args.get("page",1),len(li),request.path,request.args,per_page_count=10)
    # print(request.args)
    index_list = li[pager_obj.start:pager_obj.end]
    html = pager_obj.page_html()

    # ===============保留当前的跳转路径的条件=============
    get_dict = request.args.to_dict()  # {'page': '2', 'name': '9'}
    path = urlencode(get_dict)  # 转化成urlencode格式的
    get_dict["_list_filter"] = path
    path = urlencode(get_dict)  # 转化成urlencode格式的

    print(path)  #    page=5&aaa=1&_list_filter=page%3D5%26aaa%3D1
    print(get_dict)  # {'page': '10', '_list_filter': 'page=10'}
    return render_template("pager.html",index_list=index_list, html = html,condition=path)

@app.route('/add',methods=["GET","POST"])
def add():
    if request.method =="GET":
        return render_template("add.html")
    else:
        url = request.args.get("_list_filter")
        # print(url)
        return redirect("/pager?%s"%url)
if __name__ == '__main__':
    app.run(debug=True)

2、pager.html

<a href="/add?{{ condition }}"><button class="btn btn-primary">添加</button></a>

3、add.html

<form action="" method="post">
    <input type="text">
    <input type="submit" value="提交">
</form>

 

posted on 2017-03-03 13:52  vmaze  阅读(157)  评论(0编辑  收藏  举报

导航