📅  最后修改于: 2023-12-03 15:22:10.842000             🧑  作者: Mango
本项目是一个基于 Django 开发的大学管理系统。它提供了完整的管理和查询功能,包括学生、课程、教师和成绩的管理和查询。本文将介绍这个项目的实现细节和基本架构。
本项目是一个基于 Python 语言和 Django 框架开发的项目。所需的开发环境如下:
本项目的数据库模型如下:
class Student(models.Model):
"""学生模型"""
name = models.CharField(max_length=50)
gender = models.BooleanField(choices=((False, '女'), (True, '男')))
department = models.CharField(max_length=10)
major = models.CharField(max_length=20)
grade = models.IntegerField()
class Course(models.Model):
"""课程模型"""
name = models.CharField(max_length=50)
teacher = models.CharField(max_length=20)
credit = models.IntegerField()
class Grade(models.Model):
"""成绩模型"""
student = models.ForeignKey(Student, on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
score = models.IntegerField()
这里定义了三个数据库模型:学生(Student)、课程(Course)和成绩(Grade)。其中,学生和课程是两个独立的模型,成绩是学生和课程之间的关系模型。
本项目提供了以下功能:
本项目的页面展示如下:
本项目的代码结构如下:
management_system/
├── apps/ # 应用模块
│ ├── course/ # 课程模块
│ ├── grade/ # 成绩模块
│ └── student/ # 学生模块
├── management_system/ # 项目配置
├── templates/ # 网页模板
│ ├── course/ # 课程模块模板
│ ├── grade/ # 成绩模块模板
│ └── student/ # 学生模块模板
├── static/ # 静态文件
│ ├── css/ # CSS文件
│ ├── js/ # JavaScript文件
│ └── img/ # 图片文件
├── db.sqlite3 # 默认数据库文件
├── manage.py # Django命令行工具
└── requirements.txt # 依赖库列表
在 settings.py
文件中配置数据库信息:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'db_password',
'HOST': 'db_host',
'PORT': 'db_port',
}
}
本项目的 URL 路由如下:
from django.urls import path
from .apps.student.views import StudentListView, StudentCreateView, StudentDetailView, \
StudentUpdateView, StudentDeleteView
from .apps.course.views import CourseListView, CourseCreateView, CourseDetailView, \
CourseUpdateView, CourseDeleteView
from .apps.grade.views import GradeListView, GradeCreateView, GradeDetailView, \
GradeUpdateView, GradeDeleteView
urlpatterns = [
path('student/', StudentListView.as_view(), name='student_list'),
path('student/add/', StudentCreateView.as_view(), name='student_create'),
path('student/<int:pk>/', StudentDetailView.as_view(), name='student_detail'),
path('student/<int:pk>/update/', StudentUpdateView.as_view(), name='student_update'),
path('student/<int:pk>/delete/', StudentDeleteView.as_view(), name='student_delete'),
path('course/', CourseListView.as_view(), name='course_list'),
path('course/add/', CourseCreateView.as_view(), name='course_create'),
path('course/<int:pk>/', CourseDetailView.as_view(), name='course_detail'),
path('course/<int:pk>/update/', CourseUpdateView.as_view(), name='course_update'),
path('course/<int:pk>/delete/', CourseDeleteView.as_view(), name='course_delete'),
path('grade/', GradeListView.as_view(), name='grade_list'),
path('grade/add/', GradeCreateView.as_view(), name='grade_create'),
path('grade/<int:pk>/', GradeDetailView.as_view(), name='grade_detail'),
path('grade/<int:pk>/update/', GradeUpdateView.as_view(), name='grade_update'),
path('grade/<int:pk>/delete/', GradeDeleteView.as_view(), name='grade_delete'),
]
本项目使用 Django 提供的类视图函数来实现功能,代码如下:
from django.shortcuts import render
from django.views.generic import ListView, CreateView, DetailView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.db.models import Q
from .models import Student, Course, Grade
# 学生模块
class StudentListView(ListView):
model = Student
template_name = 'student/student_list.html'
class StudentCreateView(CreateView):
model = Student
fields = ['name', 'gender', 'department', 'major', 'grade']
template_name = 'student/student_form.html'
success_url = reverse_lazy('student_list')
class StudentDetailView(DetailView):
model = Student
template_name = 'student/student_detail.html'
class StudentUpdateView(UpdateView):
model = Student
fields = ['name', 'gender', 'department', 'major', 'grade']
template_name = 'student/student_form.html'
success_url = reverse_lazy('student_list')
class StudentDeleteView(DeleteView):
model = Student
template_name = 'student/student_confirm_delete.html'
success_url = reverse_lazy('student_list')
# 课程模块
class CourseListView(ListView):
model = Course
template_name = 'course/course_list.html'
class CourseCreateView(CreateView):
model = Course
fields = ['name', 'teacher', 'credit']
template_name = 'course/course_form.html'
success_url = reverse_lazy('course_list')
class CourseDetailView(DetailView):
model = Course
template_name = 'course/course_detail.html'
class CourseUpdateView(UpdateView):
model = Course
fields = ['name', 'teacher', 'credit']
template_name = 'course/course_form.html'
success_url = reverse_lazy('course_list')
class CourseDeleteView(DeleteView):
model = Course
template_name = 'course/course_confirm_delete.html'
success_url = reverse_lazy('course_list')
# 成绩模块
class GradeListView(ListView):
model = Grade
template_name = 'grade/grade_list.html'
def get_queryset(self):
queryset = super().get_queryset()
q = self.request.GET.get('q')
if q:
queryset = queryset.filter(
Q(student__name__icontains=q) |
Q(course__name__icontains=q) |
Q(course__teacher__icontains=q)
)
return queryset
class GradeCreateView(CreateView):
model = Grade
fields = ['student', 'course', 'score']
template_name = 'grade/grade_form.html'
success_url = reverse_lazy('grade_list')
class GradeDetailView(DetailView):
model = Grade
template_name = 'grade/grade_detail.html'
class GradeUpdateView(UpdateView):
model = Grade
fields = ['student', 'course', 'score']
template_name = 'grade/grade_form.html'
success_url = reverse_lazy('grade_list')
class GradeDeleteView(DeleteView):
model = Grade
template_name = 'grade/grade_confirm_delete.html'
success_url = reverse_lazy('grade_list')
本项目使用 Django 提供的模板语言来实现网页模板,代码如下:
{% extends 'base.html' %}
{% block page_title %}学生管理系统{% endblock %}
{% block content %}
<h1>学生列表</h1>
<a href="{% url 'student_create' %}" class="btn btn-primary mb-3">添加学生</a>
<form action="{% url 'student_list' %}" method="get">
<div class="input-group">
<input type="text" class="form-control" name="q" placeholder="请输入查询内容" value="{{ request.GET.q }}">
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit">查询</button>
</div>
</div>
</form>
<table class="table table-striped">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年级</th>
<th>专业</th>
<th>学院</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for student in object_list %}
<tr>
<td>{{ student.id }}</td>
<td>{{ student.name }}</td>
<td>{% if student.gender %}男{% else %}女{% endif %}</td>
<td>{{ student.grade }}</td>
<td>{{ student.major }}</td>
<td>{{ student.department }}</td>
<td>
<a href="{% url 'student_detail' student.id %}" class="btn btn-primary btn-sm">查看</a>
<a href="{% url 'student_update' student.id %}" class="btn btn-secondary btn-sm">修改</a>
<a href="{% url 'student_delete' student.id %}" class="btn btn-danger btn-sm text-white" onclick="return confirm('确认要删除吗?')">删除</a>
</td>
</tr>
{% empty %}
<tr>
<td colspan="7" class="text-center">暂无数据</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
本项目依赖以下 Python 库:
可以使用以下命令来安装这些库:
pip install -r requirements.txt
在本文中,我们介绍了一个使用 Django 开发的大学管理系统。我们学习了这个项目的实现细节和基本架构,包括项目的数据库模型、功能实现、页面展示、代码实现和网页模板。
本项目展示了使用 Django 开发一个完整的 Web 应用程序的过程。通过本项目的学习,可以帮助程序员更深入地理解 Django 框架的应用和使用,同时也可以帮助学生更好地学习和管理自己的学习情况。