一. 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

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>Title</title>
</head>
<div>
<a href="/add_classes.html">添加</a>
</div>
<table border="1">
<thead>
<tr>
<th>ID</th>
<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>Title</title>
</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>Title</title>
</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>Title</title>
</head>
<div><a href="/add_students.html">添加</a></div>
<body>
<table border=1>
<thead>
<tr>
<th>ID</th>
<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>Title</title>
</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>Title</title>
</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页面文件

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="set_teacher.html?nid={{ class_obj.id }}">
{% csrf_token %}
<select multiple name="teacher_id">
{% for row in all_teacher_list %}
{% if row in class_teacher_list %}
<option value="{{ row.id }}" selected="selected">{{ row.TeacherName}}</option>
{% else %}
<option value="{{ row.id }}">{{ row.TeacherName}}</option>
{% endif %}
{% endfor %}
</select>
<input type="submit" value="提交">
</form>
</body>
</html>

set_teacher.html

Django_学生管理系统的更多相关文章

  1. 【IOS开发笔记02】学生管理系统

    端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...

  2. C程序范例(2)——学生管理系统”链表“实现

    1.对于学生管理系统,能够实现的方法有许多,但是今天我们用链表的方法来实现.虽然初学者很可能看不懂,但是不要紧,这是要在整体的系统的学习完C语言之后,我才编写出的程序.所以大家不必要担心.在这里与大家 ...

  3. jsp学习之基于mvc学生管理系统的编写

    mvc开发模式:分别是 model层 view层 Control层 在学生管理系统中,model层有学生实体类,数据访问的dao层,view层主要是用于显示信息的界面,Control层主要是servl ...

  4. java版本的学生管理系统

    import java.awt.BorderLayout; import java.awt.Color; import java.awt.Frame; import java.awt.event.Ac ...

  5. 学生管理系统-火车订票系统 c语言课程设计

    概要: C 语言课程设计一---学生管理系统 使使用 C 语言实现学生管理系统.系统实现对学生的基本信息和考试成绩的 管理.采用终端命令界面,作为系统的输入输出界面.采用文件作为信息存储介质. 功能描 ...

  6. Java学生管理系统项目案例

    这是一个不错的Java学生管理系统项目案例,希望能够帮到大家的学习吧. 分代码如下 package com.student.util; import java.sql.Connection; impo ...

  7. Java+Mysql+学生管理系统

    最近正在学java和数据库,想起以前写的学生管理系统,都是从网上下载,敷衍了事.闲来无事,也就自己写了一个,不过功能实现的不是很多. 开发语言:java: 开发环境:Mysql, java: 开发工具 ...

  8. JDBC学生管理系统--处理分页显示

    分页的思想: 假设一共有104条数据,每页显示10条数据: select * from student limit 0,10; 页数是index,第index页,对应的sql语句是: select * ...

  9. JDBC-简单的学生管理系统-增删改查

    结构:三层架构--mvc 三层是为了解决整个应用程序中各个业务操作过程中不同阶段的代码封装的问题,为了使程序员更加专注的处理某阶段的业务逻辑. 比如将数据库操作代码封装到一层中,提供一些方法根据参数直 ...

随机推荐

  1. 洛谷 P2709 BZOJ 3781 小B的询问

    题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求$\sum_1^Kc_i^2$的值,其中$c_i$表示数字i在[L..R]中的重复次数.小B请 ...

  2. jquery-cookie持久化

    jQuery.Cookie.js,是一个轻量级的Cookie管理插件 <script src="jQuery.1.8.3.js" type="text/javasc ...

  3. [jQuery]$.get跨域提交不发送原因

    使用 $.ajax({ url: "http://pastebin.com/embed_js.php?i=sy9gt3FR", dataType: "jsonp" ...

  4. session_start() [function.session-start]:

    我在学习PHP的,当我尝试做在session_start() - 获取有关错误信息不能发送会话cookie. 我看到在这个问题上前面的问题,可是,仍然不能确定我的错误. 假设是错误家伙? <? ...

  5. 笔记本联想(Lenovo)G40-70M加装内存和SSD固态硬盘

    笔记本联想(Lenovo)G40-70M加装内存和SSD固态硬盘 系列文章: 笔记本电脑提速之加装内存条.SSD固态硬盘.光驱位换SSD固态硬盘 笔记本ThinkPad E430c加装内存和SSD固态 ...

  6. Spark MLlib LDA 基于GraphX实现原理及源代码分析

    LDA背景 LDA(隐含狄利克雷分布)是一个主题聚类模型,是当前主题聚类领域最火.最有力的模型之中的一个,它能通过多轮迭代把特征向量集合按主题分类.眼下,广泛运用在文本主题聚类中. LDA的开源实现有 ...

  7. [Angular] Advanced DI

    In this post, we are going to see how to solve one design pattern challenge. The challenge is what w ...

  8. Project Euler:Problem 77 Prime summations

    It is possible to write ten as the sum of primes in exactly five different ways: 7 + 3 5 + 5 5 + 3 + ...

  9. hdoj--2509--Be the Winner(尼姆博弈)

    Be the Winner Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  10. B1237 [SCOI2008]配对 贪心 + dp

    我刚开始,我打眼一看:哇!网络流大水题,直接费用流板子,建边跟zz一样.结果看了一眼数据范围...gg,luogu上只能得30,直接建边就是n^2,1e5根本过不了.咋办,只能另谋出路.想不出来,看题 ...