Search code examples
pythonurlgetflasksearchbar

Flask: how to implement a general searchbar for every view, without rewriting it?


I have a Problem to implement my search bar. I have it in my base template, an get a returnvalue over the get attribute ?search=

my Problem is now, that I want to use the searchbar from every site in my project without rewrite the searchcall in every single view.

is there a way to only write it once?

If it helps, my searchbarcode:

<input class="form-control" name="search" placeholder="Search Here" autocomplete="off" autofocus="autofocus" type="text">        

Solution

  • Add an action attribute to your form tag. This will allow you to point all searches at one view.

    <form action="{{ url_for('search') }}">
    

    You can then include something along these lines with your views.

    @app.route('/search')
    def search():
        query = request.args('search')