python-Flask搭建简易登录界面

 使用Flask框架搭建一个简易的登录界面,登录成功获取token数据

1 搭建简易登录界面

代码如下

from flask import Flask, jsonify
from flask import  request
import time, hashlib

app = Flask(__name__)

login_html = '''
<html> 
<head>
<title>Login Page</title>
</head>
<body>
<form action="/doLogin" method="post">
    Account:<input type="text" name="account"><br>
    PassWord:<input type="text" name="password"><br>
<input type="submit" value="Submit">
<input type="reset" value="reset">
</form>
</body>
</html>
'''

@app.route('/', methods=['GET', 'POST'])
def login_index():
    return login_html

@app.route('/doLogin', methods=['POST'])
def do_login():
    if request.method == 'POST':
        account = request.form['account']
        password = request.form['password']
        if account == 'freePHP' and password == '123456':
            timestamp = time.time()
            prev_str = account + password +str(timestamp)
            token = hashlib.md5(prev_str.encode(encoding='UTF-8')).hexdigest()
            json_data = [{'token': token, 'user_id':101}];
            return jsonify({'data':json_data, 'result':True, 'errorMsg':''})
        else:
            return jsonify({'data':[], 'result':True, 'errorMsg':'Account and password is not matched'})


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

启动

浏览器访问http://127.0.0.1:5000
 

登录账户密码正确,获取token 

 登录账户密码输入错误:

后续操作:登录界面连接上数据库,判断是否能登录等一系列操作

注意点

问题:在进行提交密码时遇到Not Found,经查询是登录路由编写错误

改为:@app.route('/doLogin', methods=['POST'])

查看资料:flask 不能访问/login_the requested url was not found on the server. if -CSDN博客

flask - The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again - Stack Overflow

  • 13
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值