Python - 当会话变量改变时如何更改烧瓶模板?

时间:2017-09-30 15:50:32

标签: python session flask

嗨,朋友我希望我的模板html文件在session ['train']更改时显示加载微调器。以下是我尝试但没有奏效的事情:

  • html文件:

<div class="messages">
    {% block content%}
    {% if session['training'] %}
      <div class="spinner"><div class="double-bounce1"></div>
      <div class="double-bounce2"><p style="font-weight:bold;font-size:16px;margin-left:-17px;color:white;">Training</p></div>
      </div>
    </div>
    {% else %}
      <div class="messages-content"></div>
    </div>
        <div class="message-box">
      <textarea type="text" class="message-input" placeholder="Type message..."></textarea>
      <button type="submit" class="message-submit">Send</button>
    </div>
    </div>
    </div>
    {% endif %}
    {% endblock content%}   

  • Python代码:

    def retrain():
        session['training']=True
    app = Flask(__name__)
    @app.route("/")
    def home():
        if session.get('logged_in'):
            return render_template('index.html')
        else:
            return render_template('login.html')
    @app.route('/login', methods=['POST'])
    def login():
        error = False
        POST_USERNAME = str(request.form['username'])
        POST_PASSWORD = str(request.form['password'])
        Session = sessionmaker(bind=enginedb)
        s = Session()
        query = s.query(User).filter(User.username.in_([POST_USERNAME]), User.password.in_([POST_PASSWORD]) )
        result = query.first()
        if result:
            session['logged_in'] = True ##for remember me function
            flash('Login success')
            return render_template('index.html')
        else:
            flash('Login failed!')
            return render_template('login.html',error=True) 
    @app.route("/logout")
    def logout(): 
        session['logged_in'] = False
        return home()
    @app.route('/message', methods=['POST'])
    def reply():
        answer=response(request.form['msg'])
        if "train" in answer:
               retrain()
        return jsonify( { 'text': answer})

我遇到的问题是spinner加载程序仅在我刷新模板页面时显示。我希望只要 reply()中的变量“ answer ”包含“ train ”,模板就必须自动重新加载并且必须显示微调器!

1 个答案:

答案 0 :(得分:0)

问题是您正在方法reply()中更改服务器上的会话,但随后返回json,并且不返回任何会导致模板刷新或客户端上的会话更新的内容。< / p>