flask session

Solutions on MaxInterview for flask session by the best coders in the world

showing results for - "flask session"
Eamon
26 Mar 2016
1from flask import Flask, session, redirect, url_for, request
2from markupsafe import escape
3
4app = Flask(__name__)
5
6# Set the secret key to some random bytes. Keep this really secret!
7app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
8
9@app.route('/')
10def index():
11    if 'username' in session:
12        return 'Logged in as %s' % escape(session['username'])
13    return 'You are not logged in'
14
15@app.route('/login', methods=['GET', 'POST'])
16def login():
17    if request.method == 'POST':
18        session['username'] = request.form['username']
19        return redirect(url_for('index'))
20    return '''
21        <form method="post">
22            <p><input type=text name=username>
23            <p><input type=submit value=Login>
24        </form>
25    '''
26
27@app.route('/logout')
28def logout():
29    # remove the username from the session if it's there
30    session.pop('username', None)
31    return redirect(url_for('index'))
32
Ilona
11 Jun 2020
1from flask import Flask, session
2from flask.ext.session import Session
3
4app = Flask(__name__)
5# Check Configuration section for more details
6SESSION_TYPE = 'redis'
7app.config.from_object(__name__)
8Session(app)
9
10@app.route('/set/')
11def set():
12    session['key'] = 'value'
13    return 'ok'
14
15@app.route('/get/')
16def get():
17    return session.get('key', 'not set')
18
Chuck
22 Sep 2016
1session.pop('username', None)
2