吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1279|回复: 2
收起左侧

[讨论] 小白flask 学习练习 学生管理系统

[复制链接]
lihu5841314 发表于 2021-7-5 15:38
[Asm] 纯文本查看 复制代码
#导入flask模块
from flask import Flask ,render_template,redirect,request
#使用Flask 对象创建一个app对象
app = Flask(__name__)
students = [
    {'name': 'lihu',   'chinese': '65', 'math': '65', 'english': '50'},
    {'name': 'wangwu', 'chinese': '65', 'math': '65', 'english': '50'},
    {'name': 'niutou', 'chinese': '65', 'math': '65', 'english': '50'},
]

@app.route('/login',methods=['GET','POST'])
def login():
    #登录的功能
    #全栈项目 前后端不分离
    #return实现登录的逻辑
    #request 对象可以拿到浏览器传递给服务器的所有数据
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        #登录成功之后,链接数据库,校验账号密码
        print("从服务器接收到的数据",username,password)
        #登录成功之后,应该跳转到管理页面
        return   redirect('/admin')  #redirect参数是跳转的路由地址
    return  render_template('login.html')

@app.route('/admin')
def admin():
    return  render_template('admin.html',students=students)


@app.route('/add',methods =['GET','POST'])
def add():
    #复制框架源码
    if request.method == 'POST':
        username = request.form.get('username')
        chinese = request.form.get('chinese')
        math = request.form.get('math')
        englisn = request.form.get('english')
        students.append( {'name': username,   'chinese': chinese, 'math': math, 'english': englisn})
        return  redirect('/admin')
    return  render_template('add.html')

@app.route('/delete',methods=['GET','POST'])
def delete():
     #在后台需要拿到学员的名字
     username = request.args.get('name')
     print(username)
     for  stu  in  students:
         if  stu['name'] == username:
             students.remove(stu)
     print(students)
     return redirect('/admin')


@app.route('/change',methods=['GET','POST'])
def change():
    if request.method == 'POST':
        username = request.form.get('username')
        chinese = request.form.get('chinese')
        math = request.form.get('math')
        englisn = request.form.get('english')
        for  stu  in students:
             if stu['name'] == username:
                 stu['chinese'] =chinese
                 stu['math'] = math
                 stu['englisn'] = englisn
        return  redirect('/admin') 
    #先显示学员的数据,然后再浏览修改,提交到服务器保存
    username = request.args.get('name')
    print(username)
    for stu in students:
        if stu['name'] == username:
            #需要在页面渲染学生的成绩数据
            return   render_template('change.html',students=stu)




#需要实现其他的功能(例如退出,查看学生信息)等等其他的功能,该怎么实现
if __name__ == '__main__':
    app.run()
#--------------------------------------------------------------------------------------
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>[mw_shl_code=html,true]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{# 想要一个表格 重点复制框架源码 #}
    <table style="width: 400px;margin: 100px  auto" class="table">
      <thead>
        <tr>
          <th scope="col">姓名</th>
          <th scope="col">语文</th>
          <th scope="col">数学</th>
          <th scope="col">英语</th>
          <th scope="col">操作</th>
        </tr>
      </thead>
        {#
           需要将python  对象的数据显示到浏览器上 需要使用 模板语法
           jinja2(flask)  numjucks(javascript) moka(django) vue

           jinja2 可以直接在html中使用Python的逻辑  用2个大括号进行渲染 {{  }}
        #}

      <tbody>
      {% for  stu in students %}
        <tr>
              <th>{{ stu.name }}</th>
              <td>{{ stu.chinese }}</td>
              <td>{{ stu.math }}</td>
              <td>{{ stu.english }}</td>
              <td><a href="/change?name={{ stu.name }}">修改</a> <a href="/delete?name={{ stu.name }}">删除</a> </td>
        </tr>
      {% endfor %}
      </tbody>
    </table>
<div style="width: 400px;margin: 10px auto" class="container">
  <div class="row">
    <div class="col-4">
      <a href="/add" >新增学员信息</a>
    </div>
  </div>
</body>
</html>
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增学员</title>
    {#   导入框架css #}
    {#  <link rel="stylesheet" href="/static/css/bootstrip.css"> #}
</head>
<body>
<form style="width: 400px;margin: 100px auto" method="POST" >
  <div class="mb-3">
    <label for="username" class="form-label">姓名</label>
    <input type="text"
           class="form-control"
           id="username"
           name="username"
    >
    <div id="name" class="form-text"></div>

  </div>
  <div class="mb-3">
    <label for="chinese" class="form-label">语文</label>
    <input type="text" class="form-control" id="chinese" name = "chinese">
  </div>
  <div class="mb-3">
    <label for="math" class="form-label">数学</label>
    <input type="text" class="form-control" id="math" name="math">
    <div id="emailHelp" class="form-text"></div>
  </div>
  <div class="mb-3">
    <label for="englisn" class="form-label">英语</label>
    <input type="text" class="form-control" id="englisn" name = "english">
  </div>
  <div class="mb-3 form-check">
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">记住我</label>
  </div>
  <button type="submit" class="btn btn-primary">提交</button>
</form>

</body>
</html>
[Asm] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改</title>
    {#   导入框架css #}
    {#  <link rel="stylesheet" href="/static/css/bootstrip.css"> #}
</head>
<body>
<form style="width: 400px;margin: 100px auto" method="POST" >
  <div class="mb-3">
    <label for="username" class="form-label">姓名</label>
    <input type="text"
           class="form-control"
           id="username"
           name="username"
           value="{{ students['name'] }}"
    >
    <div id="name" class="form-text"></div>
  </div>
  <div class="mb-3">
    <label for="chinese" class="form-label">语文</label>
    <input type="text"
           class="form-control"
           id="chinese"
           name = "chinese"
           value="{{ students['chinese'] }}"
    >

  </div>
  <div class="mb-3">
    <label for="math" class="form-label">数学</label>
    <input type="text"
           class="form-control"
           id="math"
           name="math"
           value="{{ students['math'] }}"
    >

    <div id="emailHelp" class="form-text"></div>
  </div>
  <div class="mb-3">
    <label for="englisn" class="form-label">英语</label>
    <input type="text"
           class="form-control"
           id="englisn"
           name = "englisn"
           value="{{ students['english'] }}"
    >

  </div>
  <div class="mb-3 form-check">
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">记住我</label>
  </div>
  <button type="submit" class="btn btn-primary">提交</button>
</form>
</body>
</html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    {#   导入框架css #}
    {#  <link rel="stylesheet" href="/static/css/bootstrip.css"> #}
</head>
<body>
<form style="width: 400px;margin: 100px auto" method="POST" >
  <div class="mb-3">
    <label for="exampleInputEmail1" class="form-label">账号</label>
    <input type="text" class="form-control" id="exampleInputEmail1" name="username">
    <div id="emailHelp" class="form-text"></div>

  </div>
  <div class="mb-3">
    <label for="exampleInputPassword1" class="form-label">密码</label>
    <input type="password" class="form-control" id="exampleInputPassword1" name = "password">
  </div>
  <div class="mb-3 form-check">
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">记住我</label>
  </div>
  <button type="submit" class="btn btn-primary">登录</button>
</form>

</body>
</html>[/mw_shl_code]

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| lihu5841314 发表于 2021-7-5 15:40
小白选手   占楼
zeknight 发表于 2021-7-6 08:03
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-1 06:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表