Django_学生管理系统

一. Django简易学生管理系统

1.在pycharm中创建工程student_manage_system,添加app:student_manage

2.配置静态文件:在工程项目目录下新建目录static,并在settings.py中追加代码

STATICFILES_DIRS=(os.path.join(BASE_DIR,'static'),)

3.设计学生管理系统所需数据库

  在studnet_manage/models.py创建班级管理表、学生管理表、老师管理表

from django.db import models

# Create your models here.
class classes(models.Model):
    title = models.CharField(max_length=32)
    teacher = models.ManyToManyField("Teachers")

class student(models.Model):
    StudentName = models.CharField(max_length=32)
    age = models.IntegerField()
    gender = models.BooleanField()
    cs = models.ForeignKey("classes",on_delete=models.CASCADE)

class Teachers(models.Model):
    TeacherName = models.CharField(max_length=32)
model.py

  在终端Terminal 项目目录下执行数据表更新命令:

python manage.py makemigrations
python manage.py migrate
View Code

4.学生管理系统之班级管理

 1.为了方便分别操作班级、老师、学生相关的业务,将student_manage目录下的views.py 删掉,在student_manage目录下新建目录views,并在views目录下 新建classes.py teachers.py students.py。

 2.在classes.py 中写 get_classes, add_classes ,delete_classes ,edit_classes四个函数,完成对班级数据的增删改查

from django.shortcuts import render
from django.shortcuts import redirect
from student_manage.models import *

def get_classes(request):
    class_list = classes.objects.all()
    return render(request,'get_classes.html',{'class_list':class_list})

def add_calsses(request):
    if request.method == "GET":
        return render(request, 'add_classes.html')
    elif request.method == 'POST':
        title = request.POST.get('title')
        classes.objects.create(title=title)
        return redirect('/get_classes.html')
def delete_classes(request):
    nid=request.GET.get('nid')
    classes.objects.filter(id=nid).delete()
    return redirect('/get_classes.html')
def edit_classes(request):
    if request.method=='GET':
        nid = request.GET.get('nid','')
        obj = classes.objects.get(id=nid)
        return render(request,'edit_classes.html',{"obj":obj})
    elif request.method=='POST':
        nid = request.GET.get('nid','')
        title = request.POST.get('title')
        classes.objects.filter(id=nid).update(title=title)
        return redirect('/get_classes.html')
classes.py

 3.在urls.py 中配置url路由

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from student_manage.views import classes,students,ajax

urlpatterns = [
    #path('admin/', admin.site.urls),
    url('^get_classes.html$',classes.get_classes),
    url('^add_classes.html$',classes.add_calsses),
    url('^delete_classes.html$',classes.delete_classes),
    url('^edit_classes.html$',classes.edit_classes),
    ]
urls.py

    4.在templates目录下建立所需的html页面文件

 <1>get_classes.html

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
     <div>
        <a href="/add_classes.html">添加a>
    div>
    <table border="1">
            <thead>
                <tr>
                    <th>IDth>
                    <th>班级名称th>
                    <th>任课老师th>
                    <th>操作th>
                tr>
            thead>
            <tbody>
                 {% for row in class_list %}
                    <tr>
                        <td>
                            {{ row.id }}
                        td>
                        <td>
                            {{ row.title}}
                        td>
                        <td>
                            {% for item in row.teacher.all %}
                                <span>{{item.TeacherName}}span>
                            {% endfor %}
                        td>
                        <td>
                            <a href="/delete_classes.html?nid={{ row.id }}">删除a>
                            |
                            <a href="/delete_classes.html?nid={{ row.id }}">ajax删除a>
                            |
                            <a href="/edit_classes.html?nid={{ row.id }}">编辑a>
                            |
                            <a href="/set_teacher.html?nid={{ row.id }}">分配老师a>
                    tr>
                {% endfor %}
            tbody>
    table>
html>
get_classes.html

 <2>add_classes.html

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
     <form action="/add_classes.html" method="POST">
        {% csrf_token %}
        <input type="text" name="title" />
        <input type="submit" value="提交" />
    form>
body>
html>
add_classes.html

   <3>edit_classes.html

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
    <h3>编辑学生信息h3>
    <form method="post" action="/edit_students.html?nid={{ obj.id }}">
        {% csrf_token %}
        <p>姓名<input type="text" name="StudentName" value="{{ obj.StudentName }}"/>p>
        <p>年龄<input type="text"  name="age" value="{{ obj.age }}"/>p>
        <p>性别:
            {% if obj.gender %}
                男<input type="radio" name="gender"  checked="checked" value="1"/><input type="radio" name="gender" value="0"/>
                {% else%}
                男<input type="radio" name="gender"  value="1"/><input type="radio" name="gender" checked="checked" value="0"/>
            {% endif %}
        p>
        <p>班级:
            <select name="class_id">
                {% for row in class_list%}
                    {% if row.id == obj.cs_id %}
                        <option value="{{ row.id }}" selected="selected">{{ row.title }}option>
                    {% else %}
                        <option value="{{ row.id }}">{{ row.title }}option>
                    {% endif %}
                {% endfor %}
            select>
        p>
        <p><input type="submit" value="提交修改"/>p>
    form>
body>
html>
edit_classes.html

5.学生管理系统之学生管理

 1.在students.py 中写 get_students, add_students ,delete_students ,edit_students 四个函数,完成对学生数据的增删改查

from django.shortcuts import render
from django.shortcuts import redirect
from student_manage.models import *

def get_students(request):
    student_list=student.objects.all()
    return render(request,'get_students.html',{'student_list':student_list})
def add_students(request):
    class_list = classes.objects.all()
    if request.method == "GET":
        return render(request, 'add_students.html',{'class_list':class_list})
    if request.method == "POST":
        StudentName=request.POST.get('StudentName','')
        age=request.POST.get('age','')
        gender=request.POST.get('gender','')
        cs=request.POST.get('cs','')
        student.objects.create(StudentName=StudentName,age=age,gender=gender,cs_id=cs)
        return redirect('/get_students.html')

def delete_students(request):
    nid = request.GET.get('nid')
    student.objects.filter(id=nid).delete()
    return redirect('/get_students.html')

def edit_students(request):
    class_list = classes.objects.all()
    if request.method == 'GET':
        nid = request.GET.get('nid','')
        obj = student.objects.get(id=nid)
        print("class_list:",class_list)
        return render(request,'edit_students.html',{"class_list":class_list,"obj":obj})
    if request.method == 'POST':
        nid = request.GET.get('nid','')
        StudentName = request.POST.get('StudentName','')
        age=request.POST.get('age','')
        gender=request.POST.get('gender','')
        cs=request.POST.get('class_id','')
        print("class_id",cs)
        student.objects.filter(id=nid).update(StudentName=StudentName,age=age,gender=gender,cs_id=cs)
        return redirect('/get_students.html')
students.py

 2.在urls.py 中配置url路由

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from student_manage.views import classes,students,ajax

urlpatterns = [
    #path('admin/', admin.site.urls),
    url('^get_classes.html$',classes.get_classes),
    url('^add_classes.html$',classes.add_calsses),
    url('^delete_classes.html$',classes.delete_classes),
    url('^edit_classes.html$',classes.edit_classes),
    url('^get_students.html$',students.get_students),
    url('^add_students.html$',students.add_students),
    url('student_del_ajax.html$',ajax.student_del_ajax),
    url('^delete_students.html$',students.delete_students),
    url('^student_edit_ajax',ajax.student_edit_ajax),
    url('^edit_students.html$',students.edit_students),
    url('^set_teacher.html$',classes.set_teacher)
]
urls.py

   3.在templates目录下建立所需的html页面文件

  <1>get_students.html

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<div><a href="/add_students.html">添加a>div>
<body>
    <table border=1>
        <thead>
            <tr>
                <th>IDth>
                <th>姓名th>
                <th>年龄th>
                <th>性别th>
                <th>班级th>
                <th>操作th>
            tr>
        thead>
        <tbody>
            {% for row in student_list %}
            <tr nid="{{ row.id }}">
                <td>{{ row.id }}td>
                <td>{{ row.StudentName }}td>
                <td>{{ row.age }}td>
                {% if row.gender%}
                    <td>td>
                {% else %}
                    <td>td>
                {% endif %}
                <td>{{ row.cs.title }}td>
                <td>
                    <a href="/delete_students.html?nid={{ row.id }}">删除a>
                    |
                    <a onclick="RemoveStudent(this);" href="#">ajax删除a>
                    |
                    <a href="/edit_students.html?nid={{ row.id }}">编辑a>
                    |
                    <a onclick="EditStudent(this);" href="#">ajax编辑a>
                td>

            tr>
            {% endfor %}
        tbody>

    table>
body>
<script src="/static/jquery-3.1.1.js">script>
<script>
 function RemoveStudent(ths) {
  var nid=$(ths).parent().parent().attr('nid');
  $.ajax({
       url:'/student_del_ajax.html',
       type:'GET',
       data:{nid:nid},
       success:function (arg) {
        if (arg == 'delete successful'){
            {#window.location.reload();#}
            $(ths).parent().parent().remove();
        }
        else{
            alert(arg);
        }
       }
     })
 }
script>
html>
get_students.html

  <2>add_students.html

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
    <h3>添加学生信息h3>
    <form method="post" action="/add_students.html">
        {% csrf_token %}
        <p>姓名<input type="text" name="StudentName" placeholder="姓名"/>p>
        <p>年龄<input type="text"  name="age" placeholder="年龄"/>p>
        <p>性别:  男<input type="radio" name="gender" value="1"/><input type="radio" name="gender" value="0"/>p>
        <p>班级:
            <select name="cs">
                {% for row in class_list %}
                    <option value={{ row.id }}>{{ row.title }}option>
                {% endfor %}
            select>
        p>
        <p><input type="submit" value="提交注册"/>p>
    form>
body>
html>
add_students.html

  <3>edit_students.html

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
    <h3>编辑学生信息h3>
    <form method="post" action="/edit_students.html?nid={{ obj.id }}">
        {% csrf_token %}
        <p>姓名<input type="text" name="StudentName" value="{{ obj.StudentName }}"/>p>
        <p>年龄<input type="text"  name="age" value="{{ obj.age }}"/>p>
        <p>性别:
            {% if obj.gender %}
                男<input type="radio" name="gender"  checked="checked" value="1"/><input type="radio" name="gender" value="0"/>
                {% else%}
                男<input type="radio" name="gender"  value="1"/><input type="radio" name="gender" checked="checked" value="0"/>
            {% endif %}
        p>
        <p>班级:
            <select name="class_id">
                {% for row in class_list%}
                    {% if row.id == obj.cs_id %}
                        <option value="{{ row.id }}" selected="selected">{{ row.title }}option>
                    {% else %}
                        <option value="{{ row.id }}">{{ row.title }}option>
                    {% endif %}
                {% endfor %}
            select>
        p>
        <p><input type="submit" value="提交修改"/>p>
    form>
body>
html>
edit_students.html

 6.学生管理系统之教师分配

 1.在classes.py中增加教师分配函数set_teacher

from django.shortcuts import render
from django.shortcuts import redirect
from student_manage.models import *

def get_classes(request):
    class_list = classes.objects.all()
    return render(request,'get_classes.html',{'class_list':class_list})

def add_calsses(request):
    if request.method == "GET":
        return render(request, 'add_classes.html')
    elif request.method == 'POST':
        title = request.POST.get('title')
        classes.objects.create(title=title)
        return redirect('/get_classes.html')
def delete_classes(request):
    nid=request.GET.get('nid')
    classes.objects.filter(id=nid).delete()
    return redirect('/get_classes.html')
def edit_classes(request):
    if request.method=='GET':
        nid = request.GET.get('nid','')
        obj = classes.objects.get(id=nid)
        return render(request,'edit_classes.html',{"obj":obj})
    elif request.method=='POST':
        nid = request.GET.get('nid','')
        title = request.POST.get('title')
        classes.objects.filter(id=nid).update(title=title)
        return redirect('/get_classes.html')

def set_teacher(request):
   if request.method=='GET':
        nid = request.GET.get('nid','')
        class_obj=classes.objects.get(id=nid)
        class_teacher_list=class_obj.teacher.all()
        all_teacher_list=Teachers.objects.all()
        return render(request,'set_teacher.html',{'class_teacher_list':class_teacher_list,'all_teacher_list':all_teacher_list,'class_obj':class_obj})
   elif request.method=='POST':
       nid=request.GET.get('nid','')
       ids_str=request.POST.getlist('teacher_id','')
       # obj = classes.objects.get(id=nid)
       # obj.teacher.set(ids_str)
       obj = classes.objects.filter(id=nid).first()
       obj.teacher.set(ids_str)
       return redirect('/get_classes.html')
classes.py

   2.在templates目录下建立所需的html页面文件


"en">

    "UTF-8">
    Title


    
"post" action="set_teacher.html?nid={{ class_obj.id }}"> {% csrf_token %} "submit" value="提交">
set_teacher.html

 

转载于:https://www.cnblogs.com/wuxunyan/p/9174446.html

你可能感兴趣的:(Django_学生管理系统)