Gabriel Ferraz
Gabriel Ferraz

Reputation: 632

Python Flask - How to pass values from one route to another?

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)

Upvotes: 4

Views: 7032

Answers (2)

Sean Saito
Sean Saito

Reputation: 675

Why not make use of flask's many useful modules? They make flask an attractive microframework for speedy web development.

Flask-login, as stated above, streamlines authentication processes and manages sessions. Flask sessions also automatically stores session data for logged-in users. This allows you to implement a "Remember Me" feature in your login form.

Also, for security purposes, you would want to decorate some of your functions with @login_required, which is part of the flask-login module. This makes sure that the user is automatically redirected to the login page if he or she is not logged in already.

Here is an example of an index function that implements this:

from flask import render_template, session
from flask.ext.login import login_required

@app.route('/')
@login_required
def index():
    return render_template("index.html")

You could also use flask.ext.openidto make authentication even more convenient.

Upvotes: 1

Or Duan
Or Duan

Reputation: 13810

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, i really like it, it simple mange my login session/views and guess what? there is 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.

Upvotes: 1

Related Questions