I'm using Flask.ext.WhooshAlchemy, a Flask
wrapper for the Whoosh full text indexing & searching library.I have an index page with a search form on it, and this is the relevant part of the views.py
:
@app.route('/')
@app.route('/index')
def index():
posts = [i for i in Email.query.all()]
return render_template('index.html',
title='Home',
posts=posts,
search_form=g.search_form)
@app.before_request
def before_request():
g.search_form = SearchForm()
@app.route('/cruz')
def cruz():
u = Politician.query.get(1)
num = len(u.emails.all())
posts = u.emails.all()
return render_template('cruz.html',title='Ted Cruz',posts=posts,num=num)
from config import MAX_SEARCH_RESULTS
@app.route('/search', methods=['POST'])
def search():
if not g.search_form.validate_on_submit():
return redirect(url_for('index'))
return redirect(url_for('search_results', query=g.search_form.search.data))
@app.route('/search_results/<query>')
def search_results(query):
results = Email.query.whoosh_search(query, MAX_SEARCH_RESULTS).all()
return render_template('search_results.html',
query=query,
results=results)
This is the HTML I'm using to show the search form :
<form style="display: inline;" action="{{ url_for('search') }}" method="post" name="search">{{ g.search_form.hidden_tag() }}{{ g.search_form.search(size=20) }}<input type="submit" value="Search"></form>
I've tried both using and not using the flask global g, taking off and applying different handlers, but nothing seems to work. Whenever I enter text into the search field and hit enter, a request is made but it just ultimately redirects to index.
small update: I switched around a few things in the view.py
, like more directly referring to the form in the search function. All changes are now reflected in the current post. Now, when I enter in searches, it still routes me back to the index, but doesn't show the CSRF token in the URL - instead, it just shows /?search=QUERY
. I think the problem lies in the redirects part of the search function of views.py
, but no matter what I change it to (like render_template
) it still loops me back to index. Anyone have any ideas on how to fix this?