基本使用

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 it's 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'
View Code

相关文章:

  • 2022-01-16
  • 2021-04-14
  • 2021-09-01
  • 2022-12-23
  • 2021-10-03
  • 2021-11-24
  • 2022-12-23
猜你喜欢
  • 2021-10-20
  • 2021-06-20
  • 2022-12-23
  • 2022-03-06
  • 2022-12-23
  • 2021-07-31
相关资源
相似解决方案