It is necessary to change the value of the login_status variable in the base template that is used for all pages of the application, using python, jinja2. The variable also has a default value of "Input." If the login is successful, login_status becomes "Logout", if not - "Login". But correctly changing the variable occurs only on one page of the personal account, and on all other pages the default value continues to be used.

How to fix this situation?

base.html:

 <li class="in">{{login_status | default('Вход')}}</li> <form name="form_in" method = 'post' action = '/personal_account'> <input type="text" placeholder="Логин" id="log" name="login"/> <input type="password" placeholder="Пароль" id="pass" name="password"/> <input type="submit"/> </form> 

Python function:

 @app.route('/personal_account', methods=['POST']) def welcome(): login = request.form['login'] password = request.form['password'] login_status = u'Выйти' sidebar_login_status = 'out' if not validate_user(login, password): login_status = u'Войти' sidebar_login_status = 'in' return u'Неправильный логин!', login_status, sidebar_login_status # добавлено после Edit One user = User() user.id = login login_user(user) # ....... data = get_user_data(login) return render_template('private.html', data=data, login_status=login_status, sidebar_login_status=sidebar_login_status) 

EDIT ONE

To save user "login", the Flask-Login module is used. The User () class is used as it is by default.

The problem is that the user remains logged in for the entire session, and I need to change the value of the login_status variable in the base html template in this case.

  • see how flask-login is configured flask-login.readthedocs.io - jfs
  • See Edit One - lipton_v
  • @lipton_v why would you not just check current_user.is_authenticated in the template instead of using the variable login_status ? - Sergey Gornostaev
  • If you already have current_user.is_authenticated , then use this value that login_status wish to set. - jfs
  • If you insert a loginization check into the base template, it turns out every time a user goes to any page on the site, will the check be done? It will not be an extra load? Is it possible to make a check once, and then save the status until the event of pressing the exit button occurs? - lipton_v

1 answer 1

Flask-Login is not familiar, but why not take the value of login_status from the session:

 <a href="{{url_for('login')}}"> {{session.get('login_status', 'log in')}} </a> 

Check and change of status will be performed only when calling a special function:

 from flask import Flask, session, redirect, url_for, render_template app = Flask(__name__) @app.route('/login') def login(): login_status = session.get('login_status', 'log in') if login_status == 'log in': session['login_status'] = 'log out' elif login_status == 'log out': session['login_status'] = 'log in' return redirect(url_for('index')) @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': app.secret_key = '123' app.run(debug=True)