2016-06-17 5 views
0

にテキストを返すバックエンドのアクションとユーザ入力とボタン、私は小さなフラスコのテストを実行し、次の操作を実行しようとしています:フラスコ - ユーザー

私は2つの形式があります。

最初はユーザー入力を受け取り、送信ボタンがあります。送信ボタンが押されたら、私は1にしたいと思います。) 'doritos'と 'oreos'の2つの値を使ってバックエンドのPythonプログラムを実行してから、2)相対テキストを返す同じページ、ボタンの隣、または一部のテキストウィンドウに表示されます。

質問 - このフォームから受け取った値をどのようにしてfooに渡すことができますか?

質問 - テキスト値をユーザーに返すにはどうすればよいですか?

第2の基本的なボタンは、Pythonプログラムを呼び出して返され、何らかの相対的なテキストをユーザーに返します(成功した)。私はこれにも答えるために上記の質問を使うことができると仮定しています。

ドキュメント私は引き続きオンラインでapp.routeを使用し、結果を持つ別のページにユーザーをルーティングしています。私は同じページにユーザーを保持し、テキストを返すだけです。これがあまりにも曖昧であれば、私にRTFMを教えることができます。これを考え出すのは難しい。

私のコードについては、以下を参照してください:

のindex.html:

<form action="/foo" method="post"> 
CHIPS:<br> 
<input type="text" name="doritos"><br> 
SNACKS:<br> 
<input type="text" name="oreos"><br> 
<input type="submit" value="Submit"> 
</form> 

<form action="/coo" method="post"> 
<input type="submit" name="pita" value="pita"><br> 
<input type="submit" name="chip" value="chip"<br> 
</form> 

app.py

from flask import render_template 
from app import app 

@app.route('/') 
@app.route('/index') 
def index(): 
    return render_template("index.html", title='Home') 

@app.route('/foo') 
def foo(): 
    try: 
     ..receive user-input values.. 
     ..do some back end actions.. 
     return 'Successful.' 
    except: 
     return 'Failed.' 

@app.route('/coo') 
def coo(): 
    try: 
     if request.form['pita']: 
      ..do some back end actions.. 
      return 'Successful.' 
     elif request.form['chip']: 
      ..do some back end actions.. 
      return 'Successful.' 
    except: 
     return 'Failed.' 

答えて

1

フラスコを内蔵Jinja2を持っている(あなたはすでにしていますこれをrender_templateの形式で使用しています)、この問題の1つの簡単な解決策フォームを使用してテンプレートを作成し、データが投稿されたときにテンプレートに「成功」​​または「失敗」メッセージを渡すことができます。

は、次のindex.htmlテンプレートスニペットを考えてみましょう:

<form action="/" method="post"> 
    CHIPS:<br /> 
    <input type="text" name="doritos"><br /> 
    SNACKS:<br /> 
    <input type="text" name="oreos"><br /> 
    <input type="submit" value="submit"> 
</form> 
{% if fooResponse is not none %} 
    {{ fooResponse }} 
{% endif %} 

<form action="/" method="post"> 
    <input type="submit" name="pita" value="pita"><br /> 
    <input type="submit" name="chip" value="chip"><br /> 
</form> 
{% if cooResponse is not none %} 
    {{ cooResponse }} 
{% endif %} 

あなたのPythonその後、このような何か(あなたが///indexから移動したくないと仮定して、あなたの質問に基づいて、私は仮定しているように見えるかもしれません

@app.route('/') 
@app.route('/index', methods=['GET', 'POST']) 
def index(): 
    if request.method == "GET": 
     return render_template("index.html") 

    if request.form["submit"] == "submit": 
     doritos = request.form["doritos"] 
     oreos = request.form["oreos"] 
     success = process(doritos, oreos) 

     return render_template("index.html", fooResponse="Successful" if success else "Failed") 

    elif request.form["submit"] == "pita": 
     success = process("pita") 
     return render_template("index.html", cooResponse="Successful" if success else "Failed") 

    elif request.form["submit"] == "chip": 
     success = process("chip") 
     return render_template("index.html", cooResponse="Successful" if success else "Failed") 
+0

def index()の直後に 'return render_template(" index.html ")'を置かないと、私はできません。各フォームからの出力は非永続的で相互に排他的です。ロードindex.htmlと私はthを得るeエラーメッセージ "ブラウザ(またはプロキシ)がこのサーバが理解できなかった要求を送信しました。"しかし、 'return index_template(" index.html ")を' def index(): 'の直後に置くと、サイトが表示されますが、ボタンをクリックするとエラーメッセージ"メソッドが許可されません "が表示されます。また、上記のindex.htmlは3つのボタン(pita、chip、submit2)を作成しますか?私はちょうどピタやチップが何らかのアクションを実行するのを探していますか? 「プロセス」機能の機能とインポート方法を明確にすることができます – dpx

+0

変更されました。 'process'はあなたが投稿されたデータで何をしたいのかを表すプレースホルダです。 'True'または' False'を返す必要があります。これを使用して「成功」/「失敗」を返すことができます。 –