学生信息管理系统 model: 学生基本信息表 学生登录信息表 学生专业成绩表
作用: 可用于初学者练习
Windows + Pycharm + sqlite + Django
https://github.com/WenNicholas/StudentSystem
1. model.py
相关配置过程在此略过,文章底部有源码获取地址,可获取源码后再看。
主要定义三个类,分别为课程表、学生信息表、登录信息表
from django.db import models
from datetime import datetime
# Create your models here.
# 课程表
class CourseModel(models.Model):
stu_id = models.CharField(max_length=15, verbose_name='学生id')
course = models.CharField(max_length=30, verbose_name='课程')
grade = models.IntegerField(default=60, verbose_name='分数')
class Meta():
db_table = 'new_course'
def __str__(self):
return '学生Id: 课程: 分数: '.format(self.stu_id, self.course, self.grade)
# 学生信息表
class StudentInformationModel(models.Model):
user_id = models.CharField(max_length=15, verbose_name='用户id')
stu_id = models.CharField(max_length=15,verbose_name='学生id')
stu_name = models.CharField(max_length=30, verbose_name='学生姓名')
stu_phone = models.CharField(max_length=20, verbose_name='学生电话')
stu_address = models.CharField(max_length=30, verbose_name='学生地址')
stu_faculty = models.CharField(max_length=20, verbose_name='院系')
stu_major = models.CharField(max_length=30, verbose_name='专业')
create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建日期')
update_time = models.DateTimeField(auto_now=True, verbose_name='最后修改日期')
time_test = models.DateTimeField(default=datetime.now)
# 取消外键(外键是可用的)
# stu_course = models.ForeignKey('CourseModel', on_delete=True)
class Meta():
db_table = 'new_studentinformation'
# 学生用户名密码表
class StudentModel(models.Model):
user_id = models.AutoField(primary_key=True)
username = models.CharField(max_length=10, verbose_name='用户名')
password = models.CharField(max_length=10, verbose_name='密码')
create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建日期')
update_time = models.DateTimeField(auto_now=True, verbose_name='最后修改日期')
time_test = models.DateTimeField(default=datetime.now)
class Meta():
db_table = 'new_student'
2、views.py
各功能逻辑实现都在此处
from django.shortcuts import render, HttpResponse, redirect, reverse
from .models import StudentModel, StudentInformationModel, CourseModel
from django.forms.models import model_to_dict
# Create your views here.
import json
# 主界面
def index(request):
context = {
'status': '未登录状态'
}
return render(request, 'studentManage/index.html', context)
# 登录界面
def login(request):
if request.method == "POST":
username = request.POST.get('username')
password = request.POST.get('password')
if not all([username, password]):
context = {
'status': '错误!用户名和密码不能为空!',
'length': 0
}
return render(request, 'studentManage/login.html', context)
else:
student = StudentModel.objects.filter(username=username, password=password)
if len(student):
# 将用户的信息存放到session中,session在中间件中是默认启用的
request.session['user'] = {
'user_id':student[0].user_id,
'username': username,
'password': password
}
context = {
'status': username,
'msg': '已登录',
'lenght': 1
}
return render(request, 'studentManage/index.html', context)
else:
context = {
'status': '用户名密码错误!请重新输入!如未注册,请先注册!'
}
return render(request, 'studentManage/login.html', context)
else:
context = {
'status': '请输入用户名和密码',
'length': 0
}
return render(request, 'studentManage/login.html', context)
#注册界面
def regist(request):
if request.method == "POST":
username = request.POST.get("username")
password = request.POST.get("password")
verif_password = request.POST.get("verif_password")
student = StudentModel.objects.filter(username=username)
# 注册验证错误信息汇总
error_message = ""
if not all([username,password,verif_password]):
error_message+="注册信息不能为空;\n"
if student:
error_message+="该用户名已存在;\n"
if password!=verif_password:
error_message+="两次密码输入不一致;\n"
# 如果存在注册信息则重定向到注册页面
if error_message:
context = {
"error": error_message
}
return render(request, 'studentManage/regist.html', context)
# 注册信息有效,注册成功
stu_data = StudentModel()
stu_data.username = username
stu_data.password = password
stu_data.save()
context = {
'status': '注册成功,请输入用户名密码登录!',
}
return render(request, 'studentManage/login.html', context)
else:
return render(request, 'studentManage/regist.html')
# 退出界面
def logout(request):
# 注销掉用户,从删除session中保存的信息
del request.session['user']
return render(request, 'studentManage/index.html')
# 增加数据 增加只能root用户或者管理员才能操作
def add(request):
if request.method == "POST":
root_information = request.session['user']
user_id = root_information['user_id']
root_id = StudentModel.objects.get(pk=user_id).user_id
if user_id == root_id:
stu_id = request.POST.get('stu_id')
stu_name = request.POST.get('stu_name')
if not all([stu_id, stu_name]):
context = {
'msg': '学号和名字有遗漏',
}
return render(request, 'studentManage/add.html', context)
stu_data = StudentInformationModel()
stu_data.user_id = root_id
stu_data.stu_id = stu_id
stu_data.stu_name = stu_name
stu_data.stu_phone = request.POST.get('stu_phone')
stu_data.stu_address = request.POST.get('stu_address')
stu_data.stu_faculty = request.POST.get('stu_faculty')
stu_data.stu_major = request.POST.get('stu_major')
stu_data.save()
context = {
'success': '增加成功',
}
return render(request, 'studentManage/add.html', context)
else:
context = {
'error': '只用root用户和管理员才能操作'
}
return render(request, 'studentManage/add.html', context)
else:
return render(request, 'studentManage/add.html')
# 查询
def select(request):
if request.method == "POST":
stu_id = request.POST.get('stu_id')
# if id=='':
# id=request.session['user']['id']
try:
stu_data = StudentInformationModel.objects.get(stu_id=stu_id)
# print(stu_data.stu_id, stu_data.stu_name, stu_data.stu_phone, stu_data.stu_address, stu_data.stu_faculty,
# stu_data.stu_major)
except:
context = {
'error': "not found studnet id: "+str(stu_id),
}
return render(request, 'studentManage/select.html', context)
stu_course = CourseModel.objects.filter(stu_id=stu_id)
dct = {}
for stu in stu_course:
dct[stu.course] = stu.grade
context = {
'stu_id': stu_data.stu_id,
'stu_name': stu_data.stu_name,
'stu_phone': stu_data.stu_phone,
'stu_address': stu_data.stu_address,
'stu_faculty': stu_data.stu_faculty,
'stu_major': stu_data.stu_major,
'course_data': dct,
'msg': True
}
return render(request, 'studentManage/select.html', context)
else:
root_information = request.session['user']
user_id = root_information['user_id']
stu_data = StudentInformationModel.objects.filter(user_id=user_id).first()
context = {
'msg': False,
'stu_id': stu_data.stu_id
}
return render(request, 'studentManage/select.html', context)
# 删除
def delete(request):
if request.method == "POST":
try:
id = int(request.POST.get('stu_id'))
# StudentInformationModel.objects.filter(stu_id=id).delete()
stu_data = StudentInformationModel.objects.filter(stu_id=id)
if len(stu_data):
stu_data.delete()
context = {
'msg': '成功删除'
}
return render(request, 'studentManage/delete.html', context)
else:
context = {
'msg': '学生学号输入错误,请确认后重新输入!'
}
return render(request, 'studentManage/delete.html', context)
except:
context = {
'msg': '学生学号输入错误,请确认后重新输入!'
}
return render(request, 'studentManage/delete.html', context)
else:
root_information = request.session['user']
user_id = root_information['user_id']
stu_data = StudentInformationModel.objects.filter(user_id=user_id).first()
context = {
'stu_id': stu_data.stu_id
}
return render(request, 'studentManage/delete.html', context)
# 修改
def update(request):
user_information = request.session['user']
user_id = user_information['user_id']
stu_data = StudentInformationModel.objects.filter(user_id=user_id).first()
context = {
'stu_id': stu_data.stu_id,
'stu_name': stu_data.stu_name,
'stu_phone': stu_data.stu_phone,
'stu_address': stu_data.stu_address,
'stu_faculty': stu_data.stu_faculty,
'stu_major': stu_data.stu_major,
}
if request.method == "POST":
stu_id = request.POST.get('stu_id')
stu_name = request.POST.get('stu_name')
stu_phone = request.POST.get('stu_phone')
stu_address = request.POST.get('stu_address')
stu_faculty = request.POST.get('stu_faculty')
stu_major = request.POST.get('stu_major')
# StudentInformationModel.objects.filter(stu_id=id).update(stu_id=stu_id, stu_name=stu_name, stu_phone=stu_phone, stu_address=stu_address, stu_faculty=stu_faculty, stu_major=stu_major)
# 或者 以下这种,对单个数据进行修改
stu_data = StudentInformationModel.objects.get(stu_id=stu_id)
stu_data.stu_id = stu_id
stu_data.stu_name = stu_name
stu_data.stu_phone = stu_phone
stu_data.stu_address = stu_address
stu_data.stu_faculty = stu_faculty
stu_data.stu_major = stu_major
stu_data.save()
context = {
'stu_id': stu_id,
'stu_name': stu_name,
'stu_phone': stu_phone,
'stu_address': stu_address,
'stu_faculty': stu_faculty,
'stu_major': stu_major,
'msg': '修改成功'
}
return render(request, 'studentManage/update.html', context)
else:
return render(request, 'studentManage/update.html', context)
3、urls.py
from django.urls import path
from studentManagement import views
from django.conf.urls import url
app_name = 'studentManager'
urlpatterns = [
path('', views.index),
path('index/', views.index, name='index'),
path('login/', views.login, name='login'),
path('regist/', views.regist, name='regist'),
path('logout/', views.logout, name='logout'),
path('add/', views.add, name='add'),
path('select/', views.select, name='select'),
path('delete/', views.delete, name='delete'),
path('update/', views.update, name='update')
]
1、学生用户名密码表“new_student” 主要包括用户id、用户名、密码以及创建时间等
2、学生信息表“new_studentinformation” 主要包括用户id、学生id、姓名、电话、地址、院系、专业、创建时间以及修改时间等
3、课程成绩表“new_course” 主要包括 学生id、课程以及分数
templates中放置html文件。这里仅仅展示一小部分代码
首页:
登录页:
登录后:
添加:
删除:
修改:
查询:
admin.py
from django.contrib import admin
from studentManagement.models import StudentInformationModel, CourseModel, StudentModel
# Register your models here.
class StudentInformationLine(admin.TabularInline):
model = CourseModel
extra = 3
class StudentInformationAdmin(admin.ModelAdmin):
# 显示的字段,先后顺序表示显示顺序
list_display = ['user_id', 'stu_id', 'stu_name', 'stu_phone', 'stu_address', 'stu_faculty', 'stu_major', 'create_time', 'update_time']
# 以哪个来过滤
list_filter = ['stu_id', 'stu_name']
# 以哪个字段来搜索,admin中就会出现一个搜索栏
search_fields = ['stu_name', 'stu_address', 'stu_faculty', 'stu_major']
list_per_page = 5
class CourseAdmin(admin.ModelAdmin):
list_display = ['stu_id', 'course', 'grade']
# inlines = [StudentInformationLine, ] # 谁的外键就写在哪边
class StudentAdmin(admin.ModelAdmin):
list_display = ['user_id', 'username', 'password', 'create_time', 'update_time']
search_fields = ['user_id', 'username']
actions_on_top = False
actions_on_bottom = True
admin.site.register(StudentInformationModel, StudentInformationAdmin)
admin.site.register(CourseModel, CourseAdmin)
admin.site.register(StudentModel, StudentAdmin)
github链接:https://github.com/WenNicholas/StudentSystem
参考链接:
https://github.com/Gscsd8527/StudentSystem
https://github.com/Yourdaylight/Django-Projects/tree/master/StudentSystem-master
https://blog.csdn.net/Demonslzh/article/details/107858450