0

I'm developing app with Python, flask, Flask-SQLalchemy, Currently implemented sort function, and search function.

This is the example of search function. get from number, and have a filter to query, then return the result to index.html

@bp.route('/', methods=['POST'])
def post():
    number:str = request.form.get('number')
    number_int = int(number)
    posts = Post.query.filter(Post.number > number_int)
    return render_template('index.html', posts=posts)

This is example of sort function. If a select option is selected, it send a request, and it also return result to index.html

@bp.route('/', methods=['GET'])
def get():
    sort_by = request.args.get("sort_by")
    if sort_by== "number":
        posts = Post.query.order_by('number').all()
    return render_template('index.html', posts=posts)

But if I operate search, then sort, the result on the screen is sorted all records rather than search result + sorted, this is simply because posts = Post.query.order_by('number').all() is override the search result.

So how could I keep the result of search? use a session and save posts there after search is done? then reuse it and sort?

davidism
  • 121,510
  • 29
  • 395
  • 339

0 Answers0