Flask structure and message form structure

In Flask, you write the route above the method declaration as follows:

@app.route('/search/<location>/')
def search():
  return render_template('search.html')

However, in HTML, how the form will publish the URL in this way

www.myapp.com/search?location=paris

the latter seems to return 404 from the application where

www.myapp.com/search/london

will return as expected.

I'm sure there is a simple piece of the puzzle that I don’t get, but, of course, the routing mechanism will consider the query string parameters to meet the requirements of the rules.

If this is not the best solution for this scenario, since I am sure that 90% of developers should come to this point ...

early.

+5
source share
1 answer

. URL. , , request.args ( GET), request.form (POST) request.values ( ).

- , :

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

, , , , :

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)

.

+9

All Articles