• ORM版学员管理系统2


    一、展示学生信息

    1、url 部分

    url(r'^student_list/', app01_views.student_list, name="student_list"),

    2、视图部分

    def student_list(request):
        student_list = models.Student.objects.all()
        return render(request, "student_list.html", {"student_list": student_list})

    3、前端部分

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="x-ua-compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>学生列表</title>
    </head>
    <body>
    <a href="{% url 'add_student' %}">添加学生</a>
    <table border="1">
      <thead>
      <tr>
        <th>#</th>
        <th>id</th>
        <th>学生姓名</th>
        <th>学生班级</th>
        <th>开班日期</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      {% for student in student_list%}
      <tr>
      <td>{{ forloop.counter}}</td>
      <td>{{ student.id }}</td>
      <td>{{ student.sname }}</td>
      <td>{{ student.cid.cname }}</td>
      <td>{{ student.cid.first_day|date:'Y-m-d' }}</td>
      <td>
        <a href="{% url 'delete_student' student.id %}">删除</a>
        <a href="{% url 'edit_student' student.id %}">编辑</a>
      </td>
      </tr>
      {% endfor %}
      </tbody>
    </table>
    </body>
    </html>

    二、删除学生信息

    1、url 部分

    url(r'^delete_student/(?P<sid>d+)$', app01_views.delete_student, name="delete_student"),

    2、视图部分

    def delete_student(request, sid):
        models.Student.objects.filter(id=sid).delete()
        return redirect(reverse("student_list"))

    3、前端部分

    <a href="{% url 'delete_student' student.id %}">删除</a>

    三、添加学生信息

    1、url 部分

    url(r'^add_student/$', app01_views.add_student, name="add_student"),

    2、视图部分

    def add_student(request):
        if request.method == "POST":
            sname = request.POST.get("sname")
            class_id = request.POST.get("class_id")
            models.Student.objects.create(sname=sname, cid_id=class_id)
            return redirect(reverse("student_list"))
    
        class_list = models.Class.objects.all()
        return render(request, "add_student.html", {"class_list": class_list})

    3、前端部分

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="x-ua-compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>添加学生</title>
    </head>
    <body>
    <form action="{% url 'add_student' %}" method="post">
      {% csrf_token %}
      <p>学生姓名:<input type="text" name="sname"></p>
      <label for="class_id">所在班级:</label>
        <select name="class_id" id="class_id">
          {% for class in class_list %}
            <option value="{{ class.id }}">{{ class.cname }}</option>
          {% endfor %}
        </select>
        <p><input type="submit" value="提交"></p>
    </form>
    </body>
    </html>

    四、编辑学生信息

    1、url 部分

    url(r'^edit_student/(?P<sid>d+)$', app01_views.edit_student, name="edit_student"),

    2、视图部分

    def edit_student(request, sid):
        # 获取到编辑的学生对象
        student_obj = models.Student.objects.get(id=sid)
        # 获取所有的班级数据
        class_list = models.Class.objects.all()
    
        if request.method == "POST":
            sname = request.POST.get("sname")
            class_id = request.POST.get("class_id")
            student_obj.sname = sname
            student_obj.cid_id = class_id
            student_obj.save()
            return redirect(reverse("student_list"))
    
        return render(request, "edit_student.html", {"student": student_obj, "class_list": class_list})

    3、前端部分

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="x-ua-compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>编辑学生信息</title>
    </head>
    <body>
    <form action="{% url 'edit_student' student.id %}" method="post">
      {% csrf_token %}
      <p>学生姓名:<input type="text" name="sname" value="{{ student.sname }}"></p>
      <label for="class_id">所在班级:</label>
      <select name="class_id" id="class_id">
        {% for class in class_list %}
          {% if class.id == student.cid_id %}
            <option value="{{ class.id }}" selected>{{ class.cname }}</option>
          {% else %}
            <option value="{{ class.id }}">{{ class.cname }}</option>
          {% endif %}
        {% endfor %}
      </select>
      <p><input type="submit" value="提交"></p>
    </form>
    </body>
    </html>

    五、基于对象的跨表查询

    1、一对多查询(班级表和学生表)

    正向查询(由学生表查询班级表)

    查询学生的班级信息

    >>> from app01 import models
    >>> models.Student.objects.first()
    
    <Student: Student object>
    
    >>> student_obj = models.Student.objects.first()
    >>> student_obj.cid
    
    <Class: Class object>
    
    >>> student_obj.cid.cname
    
    '全栈1期'
    
    >>> student_obj.cid_id
    
    1

    反向查询(由班级表查询学生表)

    查询班级的学生信息

  • 相关阅读:
    初赛—算法复杂度
    2-SAT
    最小生成树
    18-短信验证码接口
    17-腾讯云短信开发
    16-手机号是否存在验证接口
    15-多方式登录
    14-登录注册页面
    13-Git
    12-跨域请求详解
  • 原文地址:https://www.cnblogs.com/hzhcdhm/p/8337866.html
Copyright © 2020-2023  润新知