0

I have the following function with multiple routes possible :

@bp.route('/list/', defaults={'status': None, 'time': None, 'search': None})
@bp.route('/list/lot/', defaults={'status': None, 'search': None, 'time': None})
@bp.route('/list/lot/<string:time>/', defaults={'status': None, 'search': None})
@bp.route('/list/lot/<string:time>/<string:status>', defaults={'search': None})
@bp.route('/list/lot/<string:time>/<string:status>?search=<path:search>')
@login_required
def index(status, time, search):
    print(search)

All the routes works well, except the last one. I have the URL likes this :

http://192.168.10.88:5000/list/lot/OLDER/NEW?search=test

And I don't understand why, the print always return None.

Any ideas ?

Thanks

1
  • 2
    I think, you should get the search data through request. Not in function argument. Just import from flask import request and then request.args.get('search') inside the function Commented Oct 22, 2020 at 13:48

2 Answers 2

1

You can use request or request.query_string if you want to get the query string:

from flask import request

@bp.route('/list/', defaults={'status': None, 'time': None, 'search': None})
@bp.route('/list/lot/', defaults={'status': None, 'search': None, 'time': None})
@bp.route('/list/lot/<string:time>/', defaults={'status': None, 'search': None})
@bp.route('/list/lot/<string:time>/<string:status>', defaults={'search': None})
@bp.route('/list/lot/<string:time>/<string:status>?search=<path:search>')
@login_required
def index(status, time, search):
    print(request.query_string, request.args.get('search'), time, status)
    #  b'search=test' test OLDER NEW
    return 'OK'

remarks how I used request to get the value of search : request.args.get('search').

Here is another approach that I find simpler and cleaner:

@bp.route('/list/lot/parameters')
@login_required
def index():
    print(request.args.get('time'), request.args.get('status'), request.args.get('search'))
    return 'OK'

The Url look like this:

http://192.168.10.88:5000/list/lot/parameters?time=OLDER&status=NEW&search=test
Sign up to request clarification or add additional context in comments.

Comments

1

The part after the ? is the query string. I'm 99% sure Flask strips the query string off when matching routes (I couldn't confirm this in the docs hence the 1%). This means that the following urls are identical when matching a route.

http://192.168.10.88:5000/list/lot/OLDER/NEW?search=test
http://192.168.10.88:5000/list/lot/OLDER/NEW

Another way to do what (I think) you are trying to do is use the query string for all your variables.

@bp.route('/list/')
@bp.route('/list/lot/')
@login_required
def index():
    status = request.args.get('status', default=None)
    time = request.args.get('time', default=None)
    search = request.args.get('search', default=None)

    print(search)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.