用python的Django框架和mysql做有可视化界面的学校学生管理系统,功能包括学生端和教师端的登陆;学生信息、班级信息、教师信息、成绩信息的增删查改。
时间: 2024-06-09 16:10:26 浏览: 177
好的,下面是一个简单的学生管理系统的实现方案:
## 数据库设计
我们需要一个数据库来存储学生、班级、教师和成绩信息。下面是一个简单的设计:
### 学生信息表
| 字段名 | 类型 | 说明 |
| ---- | ---- | ---- |
| id | int | 学生ID,主键 |
| name | varchar(50) | 学生姓名 |
| gender | varchar(10) | 学生性别 |
| birthday | date | 学生出生日期 |
| class_id | int | 学生所在班级ID,外键 |
### 班级信息表
| 字段名 | 类型 | 说明 |
| ---- | ---- | ---- |
| id | int | 班级ID,主键 |
| name | varchar(50) | 班级名称 |
| teacher_id | int | 班主任ID,外键 |
### 教师信息表
| 字段名 | 类型 | 说明 |
| ---- | ---- | ---- |
| id | int | 教师ID,主键 |
| name | varchar(50) | 教师姓名 |
| gender | varchar(10) | 教师性别 |
| birthday | date | 教师出生日期 |
### 成绩信息表
| 字段名 | 类型 | 说明 |
| ---- | ---- | ---- |
| id | int | 成绩ID,主键 |
| student_id | int | 学生ID,外键 |
| subject | varchar(50) | 科目名称 |
| score | float | 成绩 |
## 界面设计
我们需要设计一个简单的界面来让用户操作。下面是一个简单的设计:
### 学生端
1. 登陆页面:输入学生ID和密码,点击登陆按钮。
2. 学生信息页面:显示学生的基本信息和所在班级信息。
3. 成绩查询页面:输入科目名称,点击查询按钮,显示学生在该科目的成绩。
### 教师端
1. 登陆页面:输入教师ID和密码,点击登陆按钮。
2. 教师信息页面:显示教师的基本信息和所负责的班级信息。
3. 成绩录入页面:输入学生ID、科目名称和成绩,点击保存按钮,保存成绩信息。
## 实现步骤
1. 创建一个Django项目:`django-admin startproject school`
2. 创建一个Django应用:`python manage.py startapp student`
3. 修改`school/settings.py`文件,添加应用到INSTALLED_APPS列表中:
```python
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'student',
]
```
4. 定义模型,修改`student/models.py`文件:
```python
from django.db import models
class Class(models.Model):
name = models.CharField(max_length=50)
teacher = models.ForeignKey('Teacher', on_delete=models.CASCADE)
class Student(models.Model):
name = models.CharField(max_length=50)
gender = models.CharField(max_length=10)
birthday = models.DateField()
class_id = models.ForeignKey('Class', on_delete=models.CASCADE)
class Teacher(models.Model):
name = models.CharField(max_length=50)
gender = models.CharField(max_length=10)
birthday = models.DateField()
class Score(models.Model):
student = models.ForeignKey('Student', on_delete=models.CASCADE)
subject = models.CharField(max_length=50)
score = models.FloatField()
```
5. 创建数据库表:`python manage.py makemigrations`和`python manage.py migrate`
6. 创建视图,修改`student/views.py`文件:
```python
from django.shortcuts import render
from django.http import HttpResponse
from .models import Student, Class, Teacher, Score
def student_login(request):
if request.method == 'POST':
student_id = request.POST['student_id']
password = request.POST['password']
student = Student.objects.filter(id=student_id, password=password).first()
if student:
return render(request, 'student_info.html', {'student': student})
else:
return HttpResponse('登陆失败')
else:
return render(request, 'login.html')
def student_info(request):
student_id = request.GET.get('student_id')
student = Student.objects.filter(id=student_id).first()
return render(request, 'student_info.html', {'student': student})
def score_query(request):
student_id = request.GET.get('student_id')
subject = request.GET.get('subject')
score = Score.objects.filter(student_id=student_id, subject=subject).first()
return render(request, 'score_query.html', {'score': score})
def teacher_login(request):
if request.method == 'POST':
teacher_id = request.POST['teacher_id']
password = request.POST['password']
teacher = Teacher.objects.filter(id=teacher_id, password=password).first()
if teacher:
classes = Class.objects.filter(teacher_id=teacher_id)
return render(request, 'teacher_info.html', {'teacher': teacher, 'classes': classes})
else:
return HttpResponse('登陆失败')
else:
return render(request, 'login.html')
def score_input(request):
if request.method == 'POST':
student_id = request.POST['student_id']
subject = request.POST['subject']
score = request.POST['score']
Score.objects.create(student_id=student_id, subject=subject, score=score)
return HttpResponse('保存成功')
else:
student_id = request.GET.get('student_id')
return render(request, 'score_input.html', {'student_id': student_id})
```
7. 创建模板,修改`student/templates`目录下的文件:
- `login.html`:登陆页面
```html
<h1>登陆</h1>
<form method="post">
{% csrf_token %}
<label for="student_id">学生ID</label>
<input type="text" name="student_id">
<br>
<label for="password">密码</label>
<input type="password" name="password">
<br>
<button type="submit">登陆</button>
</form>
```
- `student_info.html`:学生信息页面
```html
<h1>学生信息</h1>
<p>姓名:{{ student.name }}</p>
<p>性别:{{ student.gender }}</p>
<p>出生日期:{{ student.birthday }}</p>
<p>班级:{{ student.class_id.name }}</p>
```
- `score_query.html`:成绩查询页面
```html
<h1>成绩查询</h1>
<form method="get">
<label for="subject">科目</label>
<input type="text" name="subject">
<input type="hidden" name="student_id" value="{{ score.student_id }}">
<button type="submit">查询</button>
</form>
{% if score %}
<p>{{ score.subject }}成绩:{{ score.score }}</p>
{% endif %}
```
- `teacher_info.html`:教师信息页面
```html
<h1>教师信息</h1>
<p>姓名:{{ teacher.name }}</p>
<p>性别:{{ teacher.gender }}</p>
<p>出生日期:{{ teacher.birthday }}</p>
<h2>班级信息</h2>
<ul>
{% for class in classes %}
<li>{{ class.name }}</li>
{% endfor %}
</ul>
```
- `score_input.html`:成绩录入页面
```html
<h1>成绩录入</h1>
<form method="post">
{% csrf_token %}
<input type="hidden" name="student_id" value="{{ student_id }}">
<label for="subject">科目</label>
<input type="text" name="subject">
<br>
<label for="score">成绩</label>
<input type="text" name="score">
<br>
<button type="submit">保存</button>
</form>
```
8. 创建URL映射,修改`student/urls.py`文件:
```python
from django.urls import path
from . import views
urlpatterns = [
path('student/login/', views.student_login),
path('student/info/', views.student_info),
path('student/score/', views.score_query),
path('teacher/login/', views.teacher_login),
path('teacher/score/', views.score_input),
]
```
9. 修改`school/urls.py`文件,添加应用的URL映射:
```python
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('student.urls')),
]
```
10. 运行Django应用:`python manage.py runserver`
这样就完成了一个简单的学生管理系统的实现。当然,这只是一个初步的方案,实际情况下可能需要更复杂的功能和更完善的界面。
阅读全文