Flask 之 redirect
from flask import Flask, redirect, request, abort, render_template, Response, url_for ''' redirect 是flask 中的重定向 redirect 有三个参数 location 注意这里的是你@app.route('/') 不是显示/ 是跳转的index statuscode 设置状态码 response 应答对象 ''' app = Flask(__name__) @app.route('/') def index(): return Response('登陆成功') @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'GET': return render_template('login.html') if request.method == 'POST': uid = request.form.get('uid') password = request.form.get('password') if uid == 'xiaomo' and password == '123': return redirect(url_for('index')) else: abort(401) return render_template('login.html') else: return render_template('login.html') if __name__ == '__main__': app.run()
{% extends 'bash.html' %} {% block title %} 用户登录 {% endblock %} {% block header %} 用户登录 <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/login.css') }}"> {% endblock %} {% block body %} <form action="/login" method="POST"> <table> <tbody> <tr> <td>用户:</td> <td><input type="text" name="uid"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"></td> </tr> <tr align="center"> <td colspan="2"> <button type="submit">确定</button> <button type="reset">取消</button> </td> </tr> </tbody> </table> session中uid的数据: {{ session['uid'] }} {{uid}} {{password}} </form> {% endblock %}