• Django模型系统——ORM校园管理系统代码


    1.models.py

    from django.db import models
    
    # Create your models here.
    
    class Class(models.Model):
        id = models.AutoField(primary_key=True)  # 主键
        cname = models.CharField(max_length=32)  # 班级名称
        first_day = models.DateField()  # 开班时间
    
    class Student(models.Model):
        id = models.AutoField(primary_key=True)
        sname = models.CharField(max_length=32)
        cid = models.ForeignKey("Class")
        detail = models.OneToOneField(to="StudentInfo", null=True)
    
    class StudentInfo(models.Model):
        height = models.CharField(max_length=4)
        weight = models.CharField(max_length=4)
        addr = models.CharField(max_length=32,null=True)
    
    
    class Teacher(models.Model):
        tname = models.CharField(max_length=32)
        # cid = models.ManyToManyField(to="Class", related_name="teachers")
        cid = models.ManyToManyField(to="Class")
    模型系统

    2.views.py视图系统

    from django.shortcuts import render,reverse,redirect
    from app01 import models
    # Create your views here.
    
    #-----------------------------------------------班级管理--------------------------------------------------------
    def class_list(request):
        class_list = models.Class.objects.all()
        return render(request, "class_list.html", {"class_list": class_list})
    
    
    
    def delete_class(request):
        class_id = request.GET.get("class_id")
        models.Class.objects.filter(id=class_id).delete()
        return redirect(reverse("class_list"))
    
    
    
    def add_class(request):
        # 前端POST填好的新班级信息
        if request.method == "POST":
            cname = request.POST.get("cname")
            first_day = request.POST.get("first_day")
            # 还可以这么获取提交的数据,但不推荐这么写
            # data = request.POST.dict()
            # del data["csrfmiddlewaretoken"]
            # 创建新数据的两种方式
            # new_class = models.Class(cname=cname, first_day=first_day)
            # new_class.save()
            models.Class.objects.create(cname=cname, first_day=first_day)
            # 跳转到class_list
            return redirect(reverse('class_list'))
        # 返回添加班级的页面
        return render(request, "add_class.html")
    
    
    
    def edit_class(request):
        if request.method == "POST":
            class_id = request.POST.get("id")
            cname = request.POST.get("cname")
            first_day = request.POST.get("first_day")
            models.Class.objects.create(id=class_id, cname=cname, first_day=first_day)
            return redirect(reverse("class_list"))
        class_id = request.GET.get("class_id")
        class_obj = models.Class.objects.filter(id=class_id)
        if class_obj:
            class_obj = class_obj[0]
            return render(request, "edit_class.html", {"class": class_obj})
        # 找不到该条记录
        else:
            return redirect(reverse("class_list"))
    班级管理
    #-----------------------------------------------学生管理--------------------------------------------------------
    def student_list(request):
        student_list = models.Student.objects.all()
        return render(request, "student_list.html", {"student_list": student_list})
    
    
    def delete_student(request, sid):
        models.Student.objects.filter(id=sid).delete()
        return redirect(reverse("student_list"))
    
    
    
    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})
    
    
    
    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})
    学生管理
    #-----------------------------------------------老师管理--------------------------------------------------------
    def teacher_list(request):
        teacher_list = models.Teacher.objects.all()
        return render(request, "teacher_list.html", {"teacher_list": teacher_list})
    
    
    def delete_teacher(request, tid):
        models.Teacher.objects.filter(id=tid).delete()
        return redirect(reverse("teacher_list"))
    
    
    
    def add_teacher(request):
        if request.method == "POST":
            tname = request.POST.get("tname")
            class_ids = request.POST.getlist("class_id")
            new_teacher = models.Teacher.objects.create(tname=tname)
            # 查询出所有被选中的班级信息
            class_objs = models.Class.objects.filter(id__in=class_ids)
            # 将老师的授课班级设置为选中的班级, 以下四种都可以,注意什么时候加*
            new_teacher.cid.set(class_objs)
            # new_teacher.cid.add(*class_objs)
            # new_teacher.cid.add(*class_ids)
            # new_teacher.cid.set(class_ids)
            new_teacher.save()
            return redirect(reverse("teacher_list"))
        class_list = models.Class.objects.all()
        return render(request, "add_teacher.html", {"class_list": class_list})
    
    
    
    def edit_teacher(request, tid):
        teacher_obj = models.Teacher.objects.get(id=tid)
        class_list = models.Class.objects.all()
        if request.method == "POST":
            tname = request.POST.get("tname")
            class_ids = request.POST.getlist("class_id")
            # 更新老师相关信息
            teacher_obj.tname = tname
            teacher_obj.cid.set(class_ids)
            teacher_obj.save()  # 一定记得更新完要保存
            return redirect(reverse("teacher_list"))
        return render(request, "edit_teacher.html", {"class_list": class_list, "teacher": teacher_obj})
    老师管理

    3.urls.py

    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^class_list/$', views.class_list, name="class_list"),
        url(r'^delete_class/$', views.delete_class, name="delete_class"),
        url(r'^add_class/$', views.add_class, name="add_class"),
        url(r'^edit_class/$', views.edit_class, name="edit_class"),
    
        url(r'^student_list/', views.student_list, name="student_list"),
        url(r'^delete_student/(?P<sid>d+)$',views.delete_student, name="delete_student"),
        url(r'^add_student/$',views.add_student, name="add_student"),
        url(r'^edit_student/(?P<sid>d+)$',views.edit_student, name="edit_student"),
    
        url(r'^teacher_list/$',views.teacher_list, name="teacher_list"),
        url(r'^delete_teacher/(?P<tid>d+)$',views.delete_teacher, name="delete_teacher"),
        url(r'^add_teacher/$', views.add_teacher, name="add_teacher"),
        url(r'^edit_teacher/(?P<tid>d+)$',views.edit_teacher, name="edit_teacher"),
    ]
    路由系统

    4.前端代码部分(模板系统)

    <!DOCTYPE html>
    <html lang="en">
    <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</title>
    </head>
    <body>
    <a href="{% url 'add_class' %}">新页面添加</a>
        <table border="1" style=" 350px">
      {% for class in class_list %}
        <tr>
        <td>{{ forloop.counter }}</td>
        <td>{{ class.id }}</td>
        <td>{{ class.cname }}</td>
        <td>{{ class.first_day|date:'Y-m-d' }}</td>
        <td>
            <a href="{% url 'delete_class' %}?class_id={{ class.id }}">删除</a>
            <a href="{% url 'edit_class' %}?class_id={{ class.id }}">编辑</a>
        </td>
        </tr>
      {% endfor %}
    </table>
    </body>
    </html>
    class_list.html
    <!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" style=" 500px">
      <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>
    student_list.html
    <!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_teacher' %}">添加新老师</a>
    <table border="1" style=" 500px">
      <thead>
      <tr>
        <th>#</th>
        <th>id</th>
        <th>老师姓名</th>
        <th>授课班级</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      {% for teacher in teacher_list %}
        <tr>
          <td>{{ forloop.counter }}</td>
          <td>{{ teacher.id }}</td>
          <td>{{ teacher.tname }}</td>
          <td>
            {% for class in teacher.cid.all %}
              {% if forloop.last %}
                {{ class.cname }}
              {% else %}
                {{ class.cname }},
              {% endif %}
            {% endfor %}
          </td>
          <td>
            <a href="{% url 'delete_teacher' teacher.id %}">删除</a>
            <a href="{% url 'edit_teacher' teacher.id %}">编辑</a>
          </td>
        </tr>
      {% endfor %}
      </tbody>
    </table>
    </body>
    </html>
    teacher_list.html
    <!DOCTYPE html>
    <html lang="en">
    <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_class' %}" method="post">
      {% csrf_token %}
      <input type="text" value="{{ class.id }}" style="display: none">
      <p>班级名称:<input type="text" name="cname" value="{{ class.cname }}"></p>
      <p>开班日期:<input type="date" name="first_day" value="{{ class.first_day|date:'Y-m-d' }}"></p>
      <p>提交<input type="submit"></p>
    </form>
    </body>
    </html>
    add_class.html
    <!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>
    add_student.html
    <!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_teacher' %}" method="post">
      {% csrf_token %}
      <p>老师姓名:<input type="text" name="tname"></p>
      <label for="class_id">授课班级:</label>
        <select name="class_id" id="class_id" multiple>
          {% for class in class_list %}
            <option value="{{ class.id }}">{{ class.cname }}</option>
          {% endfor %}
        </select>
        <p><input type="submit" value="提交"></p>
    </form>
    </body>
    </html>
    add_teacher.html
    <!DOCTYPE html>
    <html lang="en">
    <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_class' %}" method="post">
      {% csrf_token %}
      <input type="text" value="{{ class.id }}" style="display: none">
      <p>班级名称:<input type="text" name="cname" value="{{ class.cname }}"></p>
      <p>开班日期:<input type="date" name="first_day" value="{{ class.first_day|date:'Y-m-d' }}"></p>
      <p>提交<input type="submit"></p>
    </form>
    </body>
    </html>
    edit_class.html
    <!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>
    edit_student.html
    <!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_teacher' teacher.id %}" method="post">
      {% csrf_token %}
      <p>老师姓名:<input type="text" name="tname" value="{{ teacher.tname }}"></p>
      <label for="class_id">授课班级:</label>
      <select name="class_id" id="class_id" multiple>
        {% for class in class_list %}
          {% if class in teacher.cid.all %}
            <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>
    edit_teacher

    5.其它设置

    #settings.py配置数据库信息
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': "shiyan2",
            "USER":"root",
            "PASSWORD":"",
            "HOST":"127.0.0.1",
            "PORT":3306,
        }
    }
    
    #__init__.py
    import pymysql
    pymysql.install_as_MySQLdb()
    其它
  • 相关阅读:
    unrecognized selector sent to class
    Xcode 7安装KSImageNamed 不启作用
    使用IntelliJ IDEA配置Erlang开发环境
    BN 详解和使用Tensorflow实现(参数理解)
    argparse 在深度学习中的应用
    转置卷积的详细理解
    递归该怎么写(二)
    递归该怎么写(一)
    二叉树的遍历python 代码
    经典排序的python实现
  • 原文地址:https://www.cnblogs.com/yangmingxianshen/p/8344487.html
Copyright © 2020-2023  润新知