2016-03-25 7 views
1

POSTから他のテンプレートへのメッセージを渡すことができます。特定の青写真の構造とルート/ビューの編集を参照してください。 _update_testルートにINGのPOSTFlaskリダイレクトのルートを2回入力する

、フラスコのredirect()機能。私から提供されたもの(それは私がbrowse_testsルートが入力されていることを私のデバッガで見ることができ、>> about to redirect

次に印刷し、メッセージの内容が期待されている通り参照>> about to render templateは、browse_tests経路が二度目に入力されbase_browse_test.htmlがレンダリングされているページの前に、

問題がコンソールに出力され、messageコンポーネントは、要求からなくなっているので、テンプレートは、(空のメッセージでレンダリングmessage = '')。ページがレンダリングされる前にもう一度>> about to render templateが印刷されています。

私はここで何が欠けていますか?

EDIT:ここで青写真構造は

root/webapp/__init__.pyある:(このファイルの全体がat this pastebinを見出すことができる)

def create_app('development'): 
    webapp = Flask(__name__) 
    ... 
    from .mod_tests import tests as tests_blueprint 
    webapp.register_blueprint(tests_blueprint, url_prefix='/tests') 
    return webapp 

root/webapp/mod_tests/__init__.py

from flask import Blueprint 
tests = Blueprint('tests', __name__) 
from . import manage_tests_views 

root/webapp/mod_tests/manage_tests_views.py

@tests.route('/_update_test', methods=['POST']) 
    def update_test(): 
     ... 
     print 'about to redirect' 
     return redirect(url_for('tests.browse_tests', message={"main":"foo message content"})) 


@tests.route('/browse_tests', methods=['GET']) 
@login_required 
def browse_tests(): 
    if request.args.get('message'): 
     message = request.args['message'] 
    else: 
     message = '' 
    print 'about to render template' 
    return render_template('tests/base_browse_tests.html', 
          message=message) 
  • デバッガの有無にかかわらず実行は違いはありません。

  • request.accept_mimetypesは、最初の要求(メッセージ完全な、要求に含まれるクエリ文字列)に('*/*', 1)を含むMIMEAccept dictに等しく、第二、空のクエリ文字列要求に('text/html', 1), ('image/webp', 1), ('application/xhtml+xml', 1), ('application/xml', 0.9), ('*/*', 0.8)を含んでいます。これは何か手掛かりですか? request.cookies['session']文字列値が変更されない

  • 、dubuggerは

  • request.fullpathはまず、通知無傷の要求にu'/tests/browse_tests?notification=<encoded json>'に等しいです(私のIDEで青に回して)それがないと考えているようだが。 request.fullpathまず、通知インタクトな要求上の第二、通知無し要求

  • request.is_xhr == Trueu'/tests/browse_tests?'に等しいです。第2の通知不要の要求では、

  • request.environ['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'の2番目の要求では、Falseであり、2番目の要求には存在しません。

  • request.environ['HTTP_UPGRADE_INSECURE_REQUESTS'] is absent from the first request, and equals「1'`、第2の要求

+0

問題を再現できません。 [編集]に[mcve]を含めるようにしてください。 – davidism

答えて

0

は申し訳ありませんが、誰も、左の誤ったリダイレクトがありましたそのページで実行されるjavascriptでこれは注意するように注意してください。

この問題は、解決策が問題に全く関係していないため、終了する可能性があります。

0

であなたの青写真の構造やその使用状況

と間違って何かがあるに違いありません、私は設計図なしで私のマシン上のコードを再現しようと、それが働きました!

コードでこの行を変更して実行すると、動作することがあります。 私はこれは私が

テスト/

----hello.py

私のマシン上で何をしたかである
return redirect(url_for('.browse_tests', message={"main":"foo message content"})) 

TO this

return redirect(url_for('tests.browse_tests', message={"main":"foo message content"})) 

を使用してPOSTリクエストをしました

----テンプレート/

-------- base_browse_tests.html

hello.py

from flask import Flask, redirect, url_for, request, render_template 
from flask.ext.login import login_required 
app = Flask(__name__) 

@app.route("/") 
def hello(): 
    return "Hello World!" 


@app.route('/_update_test', methods=['POST']) 
def update_test(): 

    print '>>>>>>>>>>>>>>about to redirect' 
    message={"main":"foo message content"} 
    return redirect(url_for('browse_tests', message=message)) 


@app.route('/browse_tests', methods=['GET']) 
def browse_tests(): 

    if request.args.get('message'): 
    message = request.args['message'] 
    else: 
    message = '' 

    print '##################about to render template' 

    return render_template('base_browse_tests.html', 
         message=message) 


if __name__ == "__main__": 
    app.run(debug=True) 

base_browse_tests.html

<h1>Message: {{message}}</h1> 
平和
+0

助けてくれてありがとうが、それは私にはこれを引き起こした不注意であった。私はスクリプトに隠された間違ったjavascript rerouteを残しました。 –

関連する問題