Files
myPythonWebHomework/2026-04-14/2026-04-14_hw_2.py
2026-04-20 20:14:49 +08:00

26 lines
728 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from flask import Flask, url_for, request, redirect, session
app = Flask(__name__)
app.secret_key = 'Your_secret_key$^52@!'
@app.route('/index')
def index():
if 'username' in session:
return f'你好,{session.get("username")}'
return redirect(url_for("login"))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
return redirect(url_for('index'))
# 当发送GET请求时页面显示输入框和"登录"按钮
return '''
<form method="post">
<p><input type=text name=username>
<p><input type=submit value=登录>
</form>
'''
if __name__ == '__main__':
app.run()