第五篇 作一個用戶登陸以後查看學員信息的小例子

需求:html

1. 用戶名: oldboy 密碼: oldboy123前端

2. 用戶登陸成功以後跳轉到列表頁面flask

3. 失敗有消息提示,從新登陸後端

4.點擊學生名稱以後,能夠看到學生的詳細信息app

 

後端:post

1 from flask import Flask
 2 from flask import request
 3 from flask import render_template
 4 from flask import redirect
 5 
 6 USER = {'username': 'oldboy', 'password': "oldboy123"}
 7 
 8 STUDENT_DICT = {
 9     1: {'name': 'Old', 'age': 38, 'gender': ''},
10     2: {'name': 'Boy', 'age': 73, 'gender': ''},
11     3: {'name': 'EDU', 'age': 84, 'gender': ''},
12 }
13 
14 app = Flask(__name__)
15 
16 
17 @app.route("/login", methods=["GET", "POST"])
18 def login():
19     if request.method == "POST":
20         if request.form["username"] == USER["username"] and request.form["password"] == USER["password"]:
21             return redirect("/student_list")
22         return render_template("login.html", msg="用戶名密碼錯誤")
23 
24     return render_template("login.html", msg=None)  # 若是前端Jinja2模板中使用了msg,這裏就算是傳遞None也要出現msg
25 
26 
27 @app.route("/student_list")
28 def student():
29     return render_template("student_list.html", student=STUDENT_DICT)
30 
31 
32 @app.route("/info")
33 def student_info():
34     stu_id = int(request.args["id"])
35     stu_info = STUDENT_DICT[stu_id]
36     return render_template("student.html", student=stu_info, stu_id=stu_id)
37 
38 
39 app.run("0.0.0.0", 5000, debug=True)

前端:spa

login.html:debug

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Welcome to Old Boy EDU</title>
 6 </head>
 7 <body>
 8     <form method="post">
 9         用戶名:<input type="text" name="username">
10         密碼:<input type="text" name="password">
11         <input type="submit" value="登陸">
12         {{ msg }}
13     </form>
14 </body>
15 </html>

student_list.html:code

1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Old Boy EDU</title>
 6 </head>
 7 <body>
 8 Welcome to Old Boy EDU
 9 <table border="2xp">
10     <thead>
11         <tr>
12             <td>id</td>
13             <td>name</td>
14             <td>option</td>
15         </tr>
16     </thead>
17     <tbody>
18         {% for foo in student %}
19             <tr>
20                 <td>{{ foo }}</td>
21                 <td>{{ student[foo].name }}</td>
22                 <td><a href="/info?id={{ foo }}">詳細</a></td>
23             </tr>
24         {% endfor %}
25     </tbody>
26 </table>
27 </body>
28 </html>

student.htmlorm

1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Old Boy EDU</title>
 6 </head>
 7 <body>
 8 Welcome to Old Boy EDU
 9 <table border="1px">
10     <thead>
11     <tr>
12         <td>id</td>
13         <td>name</td>
14         <td>age</td>
15         <td>gender</td>
16     </tr>
17     </thead>
18     <tbody>
19     <tr>
20         <td>{{ stu_id }}</td>
21         <td>{{ student.name }}</td>
22         <td>{{ student["age"] }}</td>
23         <td>{{ student.get("gender") }}</td>
24     </tr>
25     </tbody>
26 </table>
27 <div><a href="/student_list">返回</a></div>
28 </body>
29 </html>
相關文章
相關標籤/搜索