url structure and form posts with Flask

送分小仙女□ 提交于 2019-12-05 10:44:58

The query parameters are not included as part of the route matching, nor are they injected into function arguments. Only the matched URL portions are injected. What you're looking for is request.args (GET query parameters), request.form (POST) or request.values (combined).

You could do something like this if you wanted to support both:

@app.route('/search/<location>')
def search(location=None):
    location = location or request.args.get('location')
    # perform search

Though, assuming you may want to search on other parameters, probably the best way to do it would be closer to:

def _search(location=None,other_param=None):
    # perform search

@app.route('/search')
def search_custom():
    location = request.args.get('location')
    # ... get other params too ...
    return _search(location=location, other params ... )

@app.route('/search/<location>')
def search_location(location):
    return _search(location=location)

And so forth.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!