(web开发)用python模拟登录

2026-02-15 03:23:59

1、python代码如下图所示:

……

def signin():

    username = request.form['username']

    password = request.form['password']

    if username=='aaa' and password=='666':

        return render_template('signin-ok.html', username=username)

    return render_template('form.html', message='Bad username or password', username=username)

……

注意,这里提前设定了用户名是aaa,密码是666。

(web开发)用python模拟登录

2、新建一个文件夹:templates,里面新建三个html文件。

(web开发)用python模拟登录

3、form.html的内容:

(web开发)用python模拟登录

4、home.html的内容:

(web开发)用python模拟登录

5、sign-ok.html的内容:

(web开发)用python模拟登录

6、用python运行python代码,三个html模版会自动调用。

这时候,出现一个链接:127.0.0.1:5000/

用浏览器访问这个链接,得到如下图的网页。

(web开发)用python模拟登录

7、进入登录页面:127.0.0.1:5000/signin。

(web开发)用python模拟登录

8、输入正确的用户名和密码。

(web开发)用python模拟登录

猜你喜欢