Python实战作业-day5

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python实战作业-day5相关的知识,希望对你有一定的参考价值。

作业需求:

用户登录注册页面

首页、注册页面、登录页面;后端存储数据是mysql数据库

 

作业代码:

 1 [email protected]~/reboot15/day5/homework$cat app.py 
 2 #!/usr/bin/env python
 3 #coding:utf-8
 4 ‘‘‘
 5 作业需求:
 6 基于mysql数据库实现用户登录模块,并实现针对于用户信息的增删改查
 7 ‘‘‘
 8 
 9 
10 from flask import Flask,render_template,request,redirect
11 import MySQLdb as mysql
12 
13 app = Flask(__name__)
14 
15 # 连接数据库的固定用语
16 conn = mysql.connect(host=127.0.0.1,user=root,passwd=123456,db=rebootye,port=3306)
17 conn.autocommit(True)
18 cursor = conn.cursor()
19 
20 # 主页并显示当前用户所有的信息
21 @app.route(/)
22 @app.route(/index/)
23 def index():
24     welcome = "Welcome to the page!"
25     sql = "select * from  user"
26     cursor.execute(sql)
27     res = cursor.fetchall()
28     return render_template("index.html",welcome=welcome,res=res)
29 
30 # 用户登录认证页面
31 @app.route(/login/,methods=[GET,POST])
32 def login():
33     if request.method == POST:
34         username = request.form.get(yourname,None)
35         password = request.form.get(yourpasswd,None)
36         sql = select * from user where username=%s and password=%s
37         if cursor.execute(sql,(username,password)):
38             res = "Welcome to login."
39             return render_template("success.html",res=res)
40         else:    
41             res = Your username and password is wrong.
42             return render_template("success.html",res=res)
43     else:
44         return render_template("login.html")
45 
46 # 用户注册插入数据页面
47 @app.route(/register/, methods=["GET","POST"])
48 def register():
49     if request.method == POST:
50         username = request.form.get(yourname)
51         password = request.form.get(yourpasswd)
52         repassword = request.form.get(reyourpasswd)
53         sex = request.form.get(sex)
54         age = request.form.get(age)
55         phone = request.form.get(phone)
56         email = request.form.get(email)
57         role = request.form.get(role)
58         if password == repassword:
59             sql = insert into user (username,password,sex,age,phone,email,role) values ("%s","%s","%s","%s","%s","%s","%s") % (username, password,sex,age,phone,email,role)
60             cursor.execute(sql)
61             res = "Congratulations on your registration is success. %s"  % username
62             return render_template("success.html",res=res)
63         else:
64             res = "Your username or password is worong."
65             return render_template("success.html",res=res)
66     return render_template("register.html")
67 
68 # 用户删除信息页面
69 @app.route(/del/,methods=[GET,POST])
70 def del_user():
71     if request.method == GET:
72         user_id = int(request.args.get(id))
73         sql = delete from user where id =%d % user_id
74         cursor.execute(sql)
75     return redirect(/index/)
76 
77 
78 # 用户更新信息页面
79 @app.route(/update/,methods=[GET,POST])
80 def update_user():
81     if request.method == GET:
82         user_id = int(request.args.get(id))
83         return render_template(update.html,user_id=user_id)
84     if request.method == POST:
85         username = request.form.get(yourname)
86         password = request.form.get(yourpasswd)
87         repassword = request.form.get(reyourpasswd)
88         user_id = request.form.get(id)
89         if password == repassword:
90             sql = update user set password=%s where id =%s %(password,user_id)
91             cursor.execute(sql)
92             return redirect(/index/)
93         else:
94             return render_template(success.html,res=Your update is failed)
95 
96 if __name__ == __main__:
97     app.run(host=0.0.0.0,port=1234,debug=True)

 

前端简单的HTML表单代码:

  1 [email protected]~/reboot15/day5/homework/templates$ll
  2 total 40
  3 -rw-r--r--  1 guanqing  staff   2.1K  8 26 16:09 index.html
  4 -rw-r--r--  1 guanqing  staff   501B  8 26 16:09 login.html
  5 -rw-r--r--  1 guanqing  staff   958B  8 26 16:09 register.html
  6 -rw-r--r--  1 guanqing  staff   203B  8 26 16:09 success.html
  7 -rw-r--r--  1 guanqing  staff   412B  8 26 16:09 update.html
  8 [email protected]~/reboot15/day5/homework/templates$cat index.html 
  9 <html>
 10     <tittle></tittle>
 11     <body>
 12     <table align="center" border="1px">
 13         <tr>
 14             <td align="center" style="width:120px; height: 21px;" valign="middle">ID</td>
 15             <td align="center" style="width:120px; height: 21px;" valign="middle">username</td>
 16             <td align="center" style="width:120px; height: 21px;" valign="middle">password</td>
 17             <td align="center" style="width:120px; height: 21px;" valign="middle">sex</td>
 18             <td align="center" style="width:120px; height: 21px;" valign="middle">age</td>
 19             <td align="center" style="width:120px; height: 21px;" valign="middle">phone</td>
 20             <td align="center" style="width:120px; height: 21px;" valign="middle">email</td>
 21             <td align="center" style="width:120px; height: 21px;" valign="middle">role</td>
 22             <td align="center" style="width:120px; height: 21px;" valign="middle">actions1</td>
 23             <td align="center" style="width:120px; height: 21px;" valign="middle">actions2</td>
 24         </tr>
 25         {% for user in res %}
 26         <tr>
 27             <td align="center" style="width:120px; height: 21px;" valign="middle">{{user[0]}}</td>
 28             <td align="center" style="width:120px; height: 21px;" valign="middle">{{user[1]}}</td>
 29             <td align="center" style="width:120px; height: 21px;" valign="middle">{{user[2]}}</td>
 30             <td align="center" style="width:120px; height: 21px;" valign="middle">{{user[3]}}</td>
 31             <td align="center" style="width:120px; height: 21px;" valign="middle">{{user[4]}}</td>
 32             <td align="center" style="width:120px; height: 21px;" valign="middle">{{user[5]}}</td>
 33             <td align="center" style="width:120px; height: 21px;" valign="middle">{{user[6]}}</td>
 34             <td align="center" style="width:120px; height: 21px;" valign="middle">{{user[7]}}</td>
 35             <td align="center" style="width:120px; height: 21px;" valign="middle"><a href="/update/?id={{user[0]}}">update</a></td>
 36             <td align="center" style="width:120px; height: 21px;" valign="middle"><a href="/del/?id={{user[0]}}">delete</a></td>
 37         </tr>
 38         {% endfor %}
 39     </table>
 40     <h1 style="text-align:center">{{welcome}}</h1>
 41     <h2 style="text-align:center"><a href="/register/">注册</a> &nbsp&nbsp<a href="/login/">登录</a></h2>
 42     </body>
 43 </html>
 44 [email protected]~/reboot15/day5/homework/templates$cat login.html 
 45 <html>
 46     <tittle></tittle>
 47     <body style="text-align:center">
 48     <h1>欢迎登陆</h1>
 49     <form action="/login/" accept-charset="UTF-8" method="post">
 50         姓名:<input placeholder="请输入名称" type="text" name="yourname"><br>
 51         密码:<input placeholder="请输入密码" type="password" name="yourpasswd"><br>
 52         <input type="submit" name="commit" value="登陆">
 53 
 54     </form>
 55     {% if error %}
 56     <h3 style="color:red">{{res}} </h3>
 57     {% else %}
 58     <h3 style="color:red"></h3>
 59     {% endif %}
 60     </body>
 61 
 62 </html>
 63 [email protected]~/reboot15/day5/homework/templates$cat register.html 
 64 <html>
 65     <tittle></tittle>
 66     <body style="text-align:center">
 67     <h1>欢迎注册</h1>
 68     <form action="/register/" accept-charset="UTF-8" method="post">
 69         姓名:<input placeholder="注册名称" type="text" name="yourname"><br>
 70         密码:<input placeholder="设置密码" type="password" name="yourpasswd"><br>
 71         密码:<input placeholder="设置密码" type="password" name="reyourpasswd"><br>
 72         性别:<input placeholder="填写性别" type="text" name="sex"><br>
 73         年龄:<input placeholder="填写年龄" type="text" name="age"><br>
 74         手机:<input placeholder="填写手机" type="text" name="phone"><br>
 75         邮件:<input placeholder="填写邮件" type="text" name="email"><br>
 76         权限:<input placeholder="填写权限" type="text" name="role"><br>
 77         <input type="submit" name="commit" value="注册">
 78 
 79     </form>
 80     {% if error %}
 81     <h3 style="color:blue">{{error}} </h3>
 82     {% else %}
 83     <h3 style="color:blue"></h3>
 84     {% endif %}
 85     </body>
 86 
 87 </html>
 88 [email protected]~/reboot15/day5/homework/templates$cat success.html 
 89 <html>
 90         <tittle></tittle>
 91         <body style="text-align:center">
 92         <h1 style="color:red">{{res}} </h1>
 93         <h2 style="text-align:center"><a href="/index">返回首页</a></h2>
 94 </html>
 95 [email protected]~/reboot15/day5/homework/templates$cat update.html 
 96 <html>
 97     <title></title>
 98     <body style="text-align:center">
 99     <form action="/update/" accept-charset="UTF-8" method="POST">
100         <input type="hidden" name="id" value="{{user_id}}">
101         <li>姓名: <input type="text" name="yourname"> </li>
102         <li>密码: <input type="password" name="yourpasswd"> </li>
103         <li>密码: <input type="password" name="reyourpasswd"></li>
104         <input type="submit" value="更新">
105     </form>
106 </body>

 

以上是关于Python实战作业-day5的主要内容,如果未能解决你的问题,请参考以下文章

Python学习day5作业-ATM和购物商城

Python实战之路-day5

饱满骑士团队第六次作业—beta冲刺+事后诸葛亮:Day5冲刺随笔

Python全栈之路Day5(补)

饱满骑士-团队作业五 α冲刺汇总

Day5_作业