Routing error with python flask search app
<p>I am trying to get a simple search function going with my flask app. I have the following code that kicks off the search</p>

<pre><code>&lt;form action="/search" method=post&gt;
 &lt;input type=text name=search value="{{ request.form.search }}"&gt;&lt;/br&gt;
 &lt;div class="actions"&gt;&lt;input type=submit value="Search"&gt;&lt;/div&gt;
&lt;/form&gt;
</code></pre>

<p>This hooks in with my search/controllers.py script that looks like this </p>

<pre><code>@search.route('/search/')
@search.route('/search/&lt;query&gt;', methods=['GET', 'POST'])
def index(query=None):
    es = current_app.config.get("es")

    q = {"query":{ "multi_match":{"fields":["name","tags","short_desc","description"],"query":query,"fuzziness":"AUTO"}}}
    matches = es.search('products', 'offerings', body=q)
    return render_template('search/results.html', services=matches['_source'])
</code></pre>

<p>Unfortunately whenever I actually search I get a routing error:</p>

<blockquote>
  <p>FormDataRoutingRedirect: A request was sent to this URL (<a href="http://localhost:8080/search" rel="nofollow">http://localhost:8080/search</a>) but a redirect was issued automatically by the routing system to "<a href="http://localhost:8080/search/" rel="nofollow">http://localhost:8080/search/</a>". The URL was defined with a trailing slash so Flask will automatically redirect to the URL with the trailing slash if it was accessed without one. Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction. Note: this exception is only raised in debug mode</p>
</blockquote>

<p>I tried changing the method to <code>methods=['POST']</code> but it made no difference. </p>

<p>Use <code>url_for('index')</code> to generate the correct url for the action.</p>

<pre><code>&lt;form action="{{ url_for('index') }}"&gt;
</code></pre>

<hr>

<p>Currently, you're submitting to the url without the trailing <code>/</code>. Flask redirects this to the route with the trailing <code>/</code>, but POST data doesn't survive redirects on many browsers, so Flask is warning you about the issue.</p>

python flask