Python Flask - How to pass values from one route to another?
Question: Hi I am new to flask and I am trying to create a simple login functionality.
Users fill out their username and password (which at this point needs to match
the username and password I hardcoded) and if their credentials are approved
they are taken to their profile page. The profile page should show the message
Hello followed by the username. The validation is working just fine and the
user is taken to the profile page but I can't pass the username from the form
(login.html) to the template "profile.html". Below follows the code. I am
sending the code that works but there is no tentative to pass the username.

Thank you!

    
    
    from flask import *
    
    SECRET_KEY = "super secret"
    
    app = Flask(__name__)
    app.config.from_object(__name__)
    
    @app.route('/')
    def index():
    return render_template('login.html')
    
    @app.route('/login', methods=['GET', 'POST'])
    def login():
        error = None
        if request.method == 'POST':
            if request.form['username'] == 'user' and request.form['password'] == 'pass':
                session['loggedin'] = True
                return redirect(url_for('profile'))
            else:
                error="Invalid credentials. Please try again."
        return render_template('login.html', error=error)
    
    @app.route('/profile')
    def profile():
        return render_template('profile.html')
    
    @app.route('/logout')
    def logout():
        session.pop('loggedin', None)
        return redirect(url_for('login'))
    
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    


Answer: I think you miss the point of your hard work login page.

What about the next page the user will choose to visit? Will you send the
username value again? of course not..

I suggest you to define a global var(session? DB data?) that contain the
current-logged-in-user-data, so you can use all user's data, not only his
username(age?posts? etc..)

One last thing, i use [flask-login](https://flask-
login.readthedocs.org/en/latest/), i really like it, it simple mange my login
session/views and guess what? there is [current_user](https://flask-
login.readthedocs.org/en/latest/#flask.ext.login.current_user) with the
current-logged-in-user-data :)

Flask-login summery:

> Flask-Login provides user session management for Flask.
>
> It handles the common tasks of logging in, logging out, and remembering your
> users’ sessions over extended periods of time.

