Frage

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">        
War es hilfreich?

Lösung

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') 

Andere Tipps

Quick update to the above, it looks like later versions of Flask needs different syntax.

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

throws

TypeError: 'ImmutableMultiDict' object is not callable

I used square brackets around the args instead and that resolved the error

@app.route('/search')
def search():
    query = request.args['search']
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top