简体   繁体   中英

Python - how to change flask template when session variable change?

Hi friend I want my template html file to display the loading spinner when session['train'] change. Here are the things I've tried but didn't work:

  • The html file:

 <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 Code:

  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}) 

The problem I'm encountering is that the spinner loader only display when I refresh my template page. I want whenever the variable " answer " in reply() contains " train ", the template must auto reload and the spinner must be displayed!

问题在于您正在使用方法reply()更改服务器上的会话,但随后返回json,而不返回任何会导致模板刷新或客户端上的会话更新的内容。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM