2016-10-18 10 views
1

私のフラスコアプリで簡単な検索機能を利用しようとしています。私は、検索をキックオフ次のコードpythonフラスコ検索アプリのルーティングエラー

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

を持っているこれは、私の検索/私は実際に検索するたびに、この

@search.route('/search/') 
@search.route('/search/<query>', 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']) 

は、残念ながら私は、ルーティングエラーが出るようになりますcontrollers.pyスクリプトでフックします。

FormDataRoutingRedirect: A request was sent to this URL (http://localhost:8080/search) but a redirect was issued automatically by the routing system to " http://localhost:8080/search/ ". 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

私は方法をmethods=['POST']に変更しようとしましたが、違いはありませんでした。

+0

URLからクエリを取得しているのにフォームデータに送信しているため、直ちにエラーが表示されますが、これは機能しません。 – davidism

答えて

2

url_for('index')を使用して、アクションの正しいURLを生成します。

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

現在、あなたは末尾/せずにURLを送信しています。 Flaskはこれを末尾の/のルートにリダイレクトしますが、POSTデータは多くのブラウザでリダイレクトされても存続しないため、Flaskはこの問題について警告しています。

0

エラーが発生すると、フォームは/ searchに投稿されていますが、ハンドラーは/ search /に設定されています。それらを同じにする。