-
情景描述:
Demo.jpg 通过访问:http://127.0.01:8000/stu/register.html,
进入学生选课系统,根据学生所在班级选择若干门课程,
提交选课后,收集表单数据并存入SQLite3数据库,
数据库中有三个表,分别为t_student, t_course, t_classroom;
-
构建思路:
1.首先要了解Django框架的MVT(Model-View-Template)设计模式 (推荐三个学习链接:)
http://morningchen.com/2015/04/08/Django-MVC/
Django_MVT模式.png
http://python.usyiyi.cn/translate/Django_111/index.html
https://code.ziqiangxuetang.com/django/django-tutorial.html
MVT.jpg
MVC.png2.理清三个表的关系:
学生信息表 t_student与班级表t_classroom关系为n:1关系
表关系.jpg 因此,models.py中三个表对象的创建代码如下:
学生信息表 t_student与课程科目表 t_course关系为n:n关系
from django.db import models
# Create your models here.
# 班级类
class Classroom(models.Model):
class_name = models.CharField(max_length=30, unique=True)
class Meta:
db_table = 't_classroom'
def __str__(self):
return 'Classroom:%s' % self.class_name
# 课程类
class Course(models.Model):
course_name = models.CharField(max_length=30, unique=True)
class Meta:
db_table = 't_course'
def __str__(self):
return 'Course:%s' % self.course_name
# 学生类
class Student(models.Model):
stu_name = models.CharField(max_length=30, unique=True)
# 学生和班级关系:一对多, 学生和课程关系:多对多
classroom = models.ForeignKey(Classroom)
course = models.ManyToManyField(Course)
class Meta:
db_table = 't_student'
def __str__(self):
return 'Student:%s' % self.stu_name
3.其他注意事项:
- 本Demo中http协议发送表单采用的是post请求,为防止403错误,通过将setting.py文件中 MIDDLEWARE 列表的
django.middleware.csrf.CsrfViewMiddleware
语句注释掉。
1.jpg
- StudentProjectSelectSystem项目下urls.py文件配置如下:
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^stu/', include('student.urls')),
]
- student模块下urls.py文件配置如下:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^register.html$', views.register_view),
url(r'^addStu', views.addStu_view)
]
- student模块下models.py完整代码:
from django.db import models
# Create your models here.
# 班级类
class Classroom(models.Model):
class_name = models.CharField(max_length=30, unique=True)
class Meta:
db_table = 't_classroom'
def __str__(self):
return 'Classroom:%s' % self.class_name
# 课程类
class Course(models.Model):
course_name = models.CharField(max_length=30, unique=True)
class Meta:
db_table = 't_course'
def __str__(self):
return 'Course:%s' % self.course_name
# 学生类
class Student(models.Model):
stu_name = models.CharField(max_length=30, unique=True)
# 学生和班级关系:一对多, 学生和课程关系:多对多
classroom = models.ForeignKey(Classroom)
course = models.ManyToManyField(Course)
class Meta:
db_table = 't_student'
def __str__(self):
return 'Student:%s' % self.stu_name
# 获取当前班级对象
def __getClassName(class_name):
try:
cls = Classroom.objects.get(class_name=class_name)
except Classroom.DoesNotExist:
cls = Classroom.objects.create(class_name=class_name)
cls.save()
return cls
# 获取课程对象列表
def __getCourseName(*course_name):
# 声明一个空列表用于存放课程对象
course_list = []
# 遍历课程列表
for i in course_name:
try:
course_name_item = Course.objects.get(course_name=i)
except Course.DoesNotExist:
course_name_item = Course.objects.create(course_name=i)
course_name_item.save()
course_list.append(course_name_item)
return course_list
def saveStuInfo(stu_name, class_name, *course_name):
# 获取当前班级对象
cls = __getClassName(class_name)
# 获取课程对象列表
course_obj = __getCourseName(*course_name)
# 创建学生对象
stu = Student(stu_name=stu_name, classroom=cls)
stu.save()
# 将课程对象列表存放在学生对象中(添加中间表)
stu.course.add(*course_obj)
stu.save()
- student模块下views.py代码:
from django.shortcuts import render
from django.http.response import HttpResponse
from . import models
# Create your views here.
def register_view(request):
return render(request, 'register.html')
def addStu_view(request):
# 获取页面请求参数
sname = request.POST.get('sname', '')
clazz = request.POST.get('clazz', '')
cournames = request.POST.getlist('courname', [])
print(sname, clazz, cournames) #测试
print(type(cournames)) # 测试
# 调用models.py中saveStuInfo方法,将数据保存到数据库中
models.saveStuInfo(sname, clazz, *cournames)
return HttpResponse('选课成功!')
- templates模版下register.html代码:
( 时间有限,界面写的比较简陋,只做初步调试用,想要丰富样式可由Bootstrap美化,文档地址 https://v3.bootcss.com/ )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ProjectSelectDemo</title>
<style>
div {
margin: 0 auto;
width: 400px;
height: 300px;
background: lightblue;
}
</style>
</head>
<body>
<div>
<h2>欢迎进入选课系统</h2>
<hr/>
<form action="/stu/addStu" method="post">
<table>
<tr>
<td>姓   名:</td>
<td><input type="text" name="sname"/></td>
</tr>
<tr>
<td>班   级:</td>
<td>
<select name="clazz">
<option value="三年级二班">三年二班</option>
<option value="一年级二班">一年级二班</option>
<option value="二年级三班">二年级三班</option>
<option value="四年级五班">四年级五班</option>
</select>
</td>
</tr>
<tr>
<td>可选课程:</td>
<td>
<input type="checkbox" name="courname" value="Html5"/>Html5
<input type="checkbox" name="courname" value="Python"/>Python
<input type="checkbox" name="courname" value="Linux"/>Linux
<input type="checkbox" name="courname" value="Mysql"/>Mysql
</td>
</tr>
<tr>
<td>
<input type="submit" value="提 交"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
-
结果测试:
前端页面展示:
result1.jpg
result2.jpg后台数据库展示:
t_student表.jpg t_classroom表.jpg t_course表.jpg 中间表.jpg
从以上四张表中可以看出:
- 来自三年级二班的Allen同学选择了Html5、Mysql两门课程;
- 来自四年级五班的tony同学选择了Linux一门课程;
- 来自一年级二班的James同学选择了Html5、Mysql、Linux、Python四门课程;
网友评论