Title

flask实现登录

from flask import Flask, request, render_template, redirect, session
from functools import wraps

app = Flask(name, template_folder='templates')
app.debug = True

字符串随便起

app.secret_key = "affedasafafqwe"

@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'

def islogin(func):
@wraps(func)
def inner(args, **kwargs):
if session.get("username"):
return func(
args, **kwargs)
return redirect('/login')

return inner

@app.route('/login', methods=['get', "post"])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if username == 'lyx' and password == '123':
session["username"] = username
return redirect('/index')
return render_template('login.html', errno='用户名或者密码错误')
return render_template('login.html')

@app.route('/index', methods=["get"])
@islogin
def index():
if request.method == 'post':
pass
return render_template('index.html')

if name == 'main':
app.run()

posted @ 2023-07-31 17:15  哈哈哈哼  阅读(38)  评论(0编辑  收藏  举报