Answers for "flask sessions"

0

flask sessions

from flask import Flask, session, redirect, url_for, request
from markupsafe import escape

app = Flask(__name__)

# Set the secret key to some random bytes. Keep this really secret!
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'

@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 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 it's there
    session.pop('username', None)
    return redirect(url_for('index'))
Posted by: Guest on November-07-2020
0

flask session

from flask import Flask, session
from flask.ext.session import Session

app = Flask(__name__)
# Check Configuration section for more details
SESSION_TYPE = 'redis'
app.config.from_object(__name__)
Session(app)

@app.route('/set/')
def set():
    session['key'] = 'value'
    return 'ok'

@app.route('/get/')
def get():
    return session.get('key', 'not set')
Posted by: Guest on April-11-2020
0

sessions in flask

@app.route("/login",methods = ["POST","GET"])  
    def login():  
        if request.method == "POST":  
            try:   
                Email = request.form["email"]
                pwd = request.form["pwd"]    
                with sqlite3.connect("Account.db") as con:  
                    cur = con.cursor()
                    print("Connection test")   
                    cur.execute("SELECT * FROM Account WHERE Email= ? and Password= ?",(Email, pwd))
                    row = cur.fetchone()
                    print("query test")  
                    while row is not None:
                        session['email']=request.form['email']  
                        print(row[1])
                        return render_template("success.html",msg = msg)
                    else:
                        msg = "sorry wrong id"
                        return render_template("failure.html",msg = msg)
            except:  
                con.rollback()  
                msg = "problem"  
if 'email' in session:
        email = session['email']   
        return render_template("view.html") 
    else:
        return '<p>Please login first</p>'
Posted by: Guest on August-17-2021
-1

sessions in flask

@app.route("/login",methods = ["POST","GET"])  
    def login():  
        if request.method == "POST":  
            try:   
                Email = request.form["email"]
                pwd = request.form["pwd"]    
                with sqlite3.connect("Account.db") as con:  
                    cur = con.cursor()
                    print("Connection test")   
                    cur.execute("SELECT * FROM Account WHERE Email= ? and Password= ?",(Email, pwd))
                    row = cur.fetchone()
                    print("query test")  
                    while row is not None:
                        session['email']=request.form['email']  
                        print(row[1])
                        return render_template("success.html",msg = msg)
                    else:
                        msg = "sorry wrong id"
                        return render_template("failure.html",msg = msg)
            except:  
                con.rollback()  
                msg = "problem"  
#In the other function do :
 if 'email' in session:
        email = session['email']   
        return render_template("view.html") 
    else:
        return '<p>Please login first</p>'
Posted by: Guest on June-29-2021

Python Answers by Framework

Browse Popular Code Answers by Language