代码改变世界

flask 实现最简单的登录功能

2019-05-04 23:13  清风软件测试开发  阅读(842)  评论(0编辑  收藏  举报

视图函数如下:

# Sample.py

from flask import Flask, render_template, url_for, request, redirect

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'hello,world'

@app.route('/user/<username>', methods=['POST', 'GET'])
def user(username):
    return 'Hello,%s' % username

@app.route('/user/login')
def login():
    return render_template('login.html')

@app.route('/user/redirect', methods=['POST'])
def redirect_to_new_url():
    username = request.form['username']
    return redirect(url_for('user',username=username))

if __name__ == '__main__':
    app.run(debug=True)

 

template 模板如下:

#login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>请登陆会员账号</title>
</head>
<body>
    <h2>请登陆您的会员账号</h2>
    <form action='{{ url_for('.redirect_to_new_url') }}' method="POST">
        <table>
            <tr>
                <td>会员名:</td>
                <td><input type="text" name='username' placeholder="Username" value="BIKMIN"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name='password' placeholder="Password"></td>
            </tr>
            <tr>
                <td><input type="submit" value="登陆"></td>
            </tr>
        </table>
    </form>
</body>
</html>

 

 原文地址https://www.cnblogs.com/wongbingming/p/6797691.html

一些学习flask比较好的博客

https://blog.csdn.net/luanpeng825485697/article/details/80934185

https://www.cnblogs.com/wangshuyang/p/8808154.html

 https://blog.csdn.net/sinat_38682860/article/details/82354342

https://www.cnblogs.com/leiziv5/category/982701.html