Created
January 15, 2012 16:33
-
-
Save longfin/1616353 to your computer and use it in GitHub Desktop.
Flask session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from http://flask.pocoo.org/docs/quickstart/#sessions | |
from flask import Flask, session, redirect, url_for, escape, request | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
if 'username' in session: | |
return 'Logged in as %s' % escape(session['username']) | |
return 'You are not logged in' | |
@app.route('/login', methods=['GET', 'POST']) | |
def login(): | |
if request.method == 'POST': | |
session['username'] = request.form['username'] | |
return redirect(url_for('index')) | |
return ''' | |
<form action="" method="post"> | |
<p><input type=text name=username> | |
<p><input type=submit value=Login> | |
</form> | |
''' | |
@app.route('/logout') | |
def logout(): | |
# remove the username from the session if its there | |
session.pop('username', None) | |
return redirect(url_for('index')) | |
# set the secret key. keep this really secret: | |
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment