Skip to content Skip to sidebar Skip to footer

How To Have A Single Search Api Using Path Parameters (no Form Used)

I have been using this view for searching a word as: db refers mongo connection (just for ref) @app.route('/') def index(): return render_template('index.html') @app.route('/w

Solution 1:

Based on my comment-suggested recommendation to not ignore the behavior of the client browser, and to follow the intent of the definition of a URI (where /foo+bar and /bar+foo must not represent the same resource), the following is all you actually require, and handles URI-encoding of the values automatically, where your original did not handle URI encoding at all, and requires no additional client-side JavaScript of any kind:

<formaction="/search"><inputname="q"></form>

This is essentially how Google's (or DuckDuckGo's, or Yahoo!'s, or…) search form operates. Default method is GET (use a query string), input field given the abbreviated "field name" q (short for query). Using a tiny bit of JS one can bypass the form-encoding and apply the query directly as the "query string" — but remember to URI/URL-encode the value/query/search terms before combining! (And that doing this may bypass any "form data" collection performed by your backing web framework, e.g. you'll need to pull out request.query_string yourself.)

Solution 2:

In flask you can get the arguments from the request itself.

from flask import request

    @app.route('/endpoint')defmyfn():
        val1 = request.args.get('arg1')
        val2 = request.args.get('arg2')

So you can do something like this in front-end:

<formaction='/word-search'method='GET'><inputtype='text'name='wordlen' /><inputtype='text'name='wordtype' /><inputtype='text'name='word' /><inputtype='submit' /></form>

And at server end,

@app.route('/words-search', methods=['GET', 'POST'])defwordsearch(word):
    wordlen = request.args.get('wordlen')
    wordtype = request.args.get('wordtype')
    word = request.args.get('word')
    ...

Post a Comment for "How To Have A Single Search Api Using Path Parameters (no Form Used)"