一、创建Django项目-StudentSystem
由于我是之前就已经创建好了项目,所以此处我只是示范一番如何创建项目
二,创建并注册应用-studentManagement
1.创建studentManagement应用
-在项目配置文件里,导入os模块
- 在控制台执行
python manage.py startapp studentManagement 红色错误的原因是:我之前已经创建了该应用 - 启动项目,访问
http://127.0.0.1:8000
2.注册studentManagement应用
- 在配置文件的INSTALLED_APPS列表里添加index应用
三,创建数据库-studentmanager
四,配置MYSQL数据库连接信息
- 在settings.py文件里配置MySQL数据库连接信息
五,配置MYSQL数据库连接模块
- 在books的__init__.py文件里设置数据库连接模块
注意:如果报错,请在控制台执行pip install pymysql 安装pymysql模块
六,创建模型
1.创建课程表模型-Course
2.创建学生信息模型-StudentInformation
3.创建学生用户模型-Student
from django.db import models
class CourseModel(models.Model):
cour_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 = 'course'
def __str__(self):
return '学生Id: 课程: 分数: '.format(self.cour_id, self.course, self.grade)
class StudentInformationModel(models.Model):
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='学生电话')
str_addr = models.TextField(verbose_name='学生地址')
stu_faculty = models.CharField(max_length=20, verbose_name='院系')
stu_major = models.CharField(max_length=30, verbose_name='专业')
class Meta():
db_table = 'studentinformation'
def __str__(self):
return self.stu_id
class StudentModel(models.Model):
stu_id = models.AutoField(primary_key=True)
username = models.CharField(max_length=10, verbose_name='用户名')
password = models.CharField(max_length=10, verbose_name='密码')
class Meta():
db_table = 'student'
def __str__(self):
return self.username
七,数据迁移,生成课程表,学生信息表和学生用户表
- 依次执行以下命令
python manage.py makemigrations
python manage.py migrate
- 查看生成表的情况
八,给课程表,学生信息表和学生用户表添加记录
1.给课程表添加记录
insert into `course` values(1,'1001','计算机基础','90');
insert into `course` values(2,'1002','大学英语IA','99');
insert into `course` values(3,'1003','大学英语IB','99');
insert into `course` values(4,'1004','中国近代史纲要','89');
insert into `course` values(5,'1005','高等数学','85');
insert into `course` values(6,'1006','体育','95');
insert into `course` values(7,'1007','大学英语2A','99');
insert into `course` values(8,'1008','大学英语2B','100');
insert into `course` values(9,'1009','思想道德修养与法律基础','97');
insert into `course` values(10,'1010','大学物理','96');
insert into `course` values(11,'1011','程序设计基础','98');
insert into `course` values(12,'1012','毛泽东思想和宗国特色社会主义理论体系','80');
insert into `course` values(13,'1013','计算机网络','87');
insert into `course` values(14,'1014','企业管理','85');
insert into `course` values(15,'1015','马克思主义基本原理概论','90');
insert into `course` values(16,'1016','形式与政策','88');
insert into `course` values(17,'1017','现代市场营销','95');
insert into `course` values(18,'1018','艺术概论','90');
insert into `course` values(19,'1019','无机化学','89');
insert into `course` values(20,'1020','工程实训','90');
- 查看课程表
2.给学生信息表添加记录
insert into `studentinformation` values (1,1,'迪丽大热巴','12345678911','四川宜宾','人工智能与大数据学院','软件技术');
insert into `studentinformation` values (2,2,'wt','12345678912','四川宜宾','人工智能与大数据学院','软件技术');
insert into `studentinformation` values (3,3,'杨潞潞','12345678913','四川广安','人工智能与大数据学院','软件技术');
insert into `studentinformation` values (4,4,'李琴琴','12345678914','四川峨眉','人工智能与大数据学院','大数据技术');
insert into `studentinformation` values (5,5,'周慧慧','12345678915','四川乐山','人工智能与大数据学院','电子信息工程技术');
insert into `studentinformation` values (6,6,'曹霖霖','12345678916','四川南充','人工智能与大数据学院','计算机应用技术');
insert into `studentinformation` values (7,7,'刘丽丽','12345678917','山东济宁','文旅学院','旅游管理');
insert into `studentinformation` values (8,8,'熊晶晶','12345678918','山东青岛','数字经济学院','电子商务');
insert into `studentinformation` values (9,9,'何晓晓','12345678919','陕西西安','数字经济学院','市场营销');
insert into `studentinformation` values (10,10,'吴磊','12345678920','河南郑州','机械工程学院','汽车制造与试验技术');
insert into `studentinformation` values (11,11,'郑小红','12345678921','河北石家庄','机械工程学院','汽车电子技术');
insert into `studentinformation` values (12,12,'冯晓宇','12345678922','贵州铜仁','智能建造学院','工程造价');
insert into `studentinformation` values (13,13,'张晓晓','12345678923','海南海口','师范学院','英语教育');
insert into `studentinformation` values (14,14,'沈莉莉','12345678924','河南沈阳','师范学院','语文教育');
insert into `studentinformation` values (15,15,'李雯雯','12345678925','湖南长沙','师范学院','学前教育');
- 查看学生信息表
3.给学生用户表添加记录
insert into `student`values (1,'迪丽大热巴','20010615');
insert into `student`values (2,'wt','19880818');
insert into `student`values (3,'杨潞潞','12345678');
insert into `student`values (4,'李琴琴','87654321');
- 查看学生用户表
九,创建模板页面
1.创建登录页面-login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<style>
.login{
text-align: center;
}
</style>
<body>
你好: {{ status }}
<form method="post" action="/student/login/">
{% csrf_token %}
<input style="text-align: center" type="text" name="username" placeholder="用户名:"/><br>
<input style="text-align: center" type="password" name="password" placeholder="密 码:"/><br>
<input class="login" type="submit" value="登录"/>
</form>
</body>
</html>
2.创建主页面-index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>页面</title>
</head>
<style>
h3{
text-align: center;
color: green;
}
.login{
text-align: center;
}
.registered{
text-align: center;
}
</style>
<body>
{% if lenght %}
<h1 style="color: blue">欢迎{{ status}},来到学生信息管理系统</h1><br>
<h3 >请选择你要执行的功能</h3>
<a href="{% url 'student:add' %}">增加学生信息</a><br>
<a href="{% url 'student:select' %}">查询学生信息</a><br>
<a href="{% url 'student:delete' %}">删除学生信息</a><br>
<a href="{% url 'student:update' %}">修改学生信息</a><br>
<br>
<a href="/student/logout/">退出</a><br>
{% else %}
<h3 style="color: green">请选择操作</h3><br>
<a href="student/login/" class="login">登录</a><br>
<a href="student/regist/" class="registered">注册</a><br>
{% endif %}
</body>
</html>
3.创建注册页面-regist.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form method="post" action="/student/regist/">
{%csrf_token %}
<input type="text" name="username" placeholder="用户名:"/><br>
<input type="password" name="password" placeholder="密 码:"/><br>
<input type="password" name="verif_password" placeholder="确认密码"><br>
{% if error %}
<h1 style="color: red">{{ error }}</h1>
{% endif %}
<input class="tj" type="submit" value="注册"/>
</form>
</body>
</html>
4.创建添加记录页面-add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>请在下列信息栏中填入学生的基本信息</title>
</head>
<body>
<h1>请在下列信息栏中填入学生的基本信息</h1>
<form method="post" action="{% url 'student:add' %}">
{% csrf_token %}
学生学号:<input type="text" name="stu_id"/><br>
学生姓名:<input type="text" name="stu_name"/><br>
{% if msg %}
<h5>{{ msg }}</h5><br>
{% endif %}
学生电话:<input type="text" name="stu_phone"/><br>
学生地址:<input type="text" name="str_addr"/><br>
学生院系:<input type="text" name="stu_faculty"/><br>
学生专业:<input type="text" name="stu_major"/><br>
<input class="bc" type="submit" value="保存"/>
{% if error %}
<h5 style="color: red">{{ error }}</h5><br>
{% endif %}
{% if sucess %}
<h5 style="color: greenyellow">{{ sucess }}</h5><br>
<a href=""></a>
{% endif %}
</form>
</body>
</html>
5.创建删除记录页面-delete.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>请输入你要想要删除的学生学号</title>
</head>
<body>
<h1>请输入你要想要删除的学生学号</h1>
<form method="post" action="/student/delete/">
{% csrf_token %}
学生学号:<input type="text" name="stu_id" placeholder={{ id }}><br>
<input class="bc" type="submit" value="删除">
</form>
{% if msg %}
{{ msg }}
{% endif %}
</body>
</html>
6.创建查询记录页面-select.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>根据学号来查询学习基本信息及选课情况</title>
</head>
<body>
<h1>根据学号来查询学习基本信息及选课情况</h1>
{% if not msg %}
{
<form method="post" action="/student/select/">
{% csrf_token %}
学生学号:<input type="text" name="stu_id" placeholder={{ id }}><br>
<input class="bc" type="submit" value="查询">
</form>
{% else %}
<ul>
<li>学号: {{ stu_id }}</li>
<li>姓名: {{ stu_name }}</li>
<li>电话: {{ stu_phone }}</li>
<li>地址: {{ str_addr }}</li>
<li>院系: {{ stu_faculty }}</li>
<li>专业: {{ stu_major }}</li>
</ul>
<hr>
<ul>
{% for k, v in course_data.items %}
<li>{{ k }}: {{ v }}</li>
{% endfor %}
</ul>
{% endif %}
{% if error %}
<h2 style="color: red">{{ error}}</h2>
{% endif %}
</body>
</html>
7.创建修改记录页面-update.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>根据学号来修改学生基本信息</title>
</head>
<body>
<h1>根据学号来修改学生基本信息</h1>
<form method="post" action="/student/update/">
{% csrf_token %}
学生学号:<input type="text" name="stu_id" placeholder={{ stu_id }}><br>
学生姓名:<input type="text" name="stu_name" placeholder={{ stu_name }}><br>
学生电话:<input type="text" name="stu_phone" placeholder={{ stu_phone }}><br>
学生地址:<input type="text" name="stu_addr" placeholder={{ stu_addr }}><br>
学生院系:<input type="text" name="stu_faculty" placeholder={{ stu_faculty }}><br>
学生专业:<input type="text" name="stu_major" placeholder={{ stu_major }}><br>
<input class="bc" type="submit" value="保存">
</form>
{% if msg %}
{{ msg }}
{% endif %}
</body>
</html>
十,设置路由
1.创建子路由
- 在index目录里创建子路由urls.py,在里面创建子路由集合urlpatterns
2.创建主路由
- 在主路由集合里添加一个路由:
path('', include('studentManagement.urls', namespace='student')),
十一,创建视图函数
** 在studentManagement的view.py里创建以下视图函数**
1.创建主页面的视图函数-index
2.创建登录页面的视图函数-login
3.创建注册页面的视图函数-regist
4.创建添加页面的视图函数-add
5.创建查询页面的视图函数-select
6.创建删除页面的视图函数-delete
7.创建修改修改页面的视图函数-update
from django.shortcuts import render, HttpResponse, redirect, reverse
from .models import StudentModel, StudentInformationModel, CourseModel
from django.forms.models import model_to_dict
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]):
return HttpResponse('错误!用户名或密码为空!')
else:
student = StudentModel.objects.filter(username=username, password=password)
if len(student):
request.session['user'] = {
'id':student[0].stu_id,
'username': username,
'password': password
}
context = {
'status': username,
'msg': '已登录',
'lenght': 1
}
return render(request, 'studentManage/index.html',context)
else:
context = {
'msg': '用户名密码错误'
}
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 = {
'sucess': '增加成功',
}
return render(request, 'studentManage/login.html', context)
else:
return render(request, 'studentManage/regist.html')
def logout(request):
del request.session['user']
return render(request, 'studentManage/index.html')
def add(request):
if request.method == "POST":
root_information = request.session['user']
id = root_information['id']
root_id = StudentModel.objects.get(pk=id).stu_id
if id == root_id:
stu_name = request.POST.get('stu_name')
if not stu_name:
context = {
'msg': '名字有遗漏',
}
return render(request, 'studentManage/add.html', context)
stu_data = StudentInformationModel()
stu_data.stu_id=id
stu_data.stu_name = stu_name
stu_data.stu_phone = request.POST.get('stu_phone')
stu_data.str_addr = request.POST.get('str_addr')
stu_data.stu_faculty =request.POST.get('stu_faculty')
stu_data.stu_major = request.POST.get('stu_major')
stu_data.save()
context = {
'sucess': '增加成功',
}
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":
id = request.POST.get('stu_id')
if id=='':
id=request.session['user']['id']
try:
stu_data = StudentInformationModel.objects.get(pk=id)
except:
context = {
'error': "not found studnet id: "+str(id),
}
return render(request, 'studentManage/select.html', context)
stu_course = CourseModel.objects.filter(cour_id=id)
dct = {}
for stu in stu_course:
dct[stu.course] = stu.grade
context = {
'stu_id': id,
'stu_name': stu_data.stu_name,
'stu_phone':stu_data.stu_phone,
'str_addr': stu_data.str_addr,
'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']
id = root_information['id']
context = {
'msg': False,
'id': id
}
return render(request, 'studentManage/select.html', context)
def delete(request):
if request.method == "POST":
id = int(request.POST.get('stu_id'))
try:
StudentInformationModel.objects.filter(stu_id=id).delete()
except:
pass
context = {
'msg': '成功删除'
}
return render(request, 'studentManage/delete.html', context)
else:
root_information = request.session['user']
id = root_information['id']
context = {
'id': id
}
return render(request, 'studentManage/delete.html', context)
def update(request):
user_information = request.session['user']
id = user_information['id']
stu_data = StudentInformationModel.objects.get(stu_id=id)
context = {
'stu_id': stu_data.stu_id,
'stu_name': stu_data.stu_name,
'stu_phone':stu_data.stu_phone,
'str_addr': stu_data.str_addr,
'stu_faculty': stu_data.stu_faculty,
'stu_major': stu_data.stu_major,
}
if request.method == "POST":
context['stu_id'] = request.POST.get('stu_id')
context['stu_name'] = request.POST.get('stu_name')
context['stu_phone'] = request.POST.get('stu_phone')
context['stu_addr'] = request.POST.get('stu_addr')
context['stu_faculty'] = request.POST.get('stu_faculty')
context['stu_major'] = request.POST.get('stu_major')
return render(request, 'studentManage/update.html', context)
else:
return render(request, 'studentManage/update.html', context)
十二,启动项目,测试效果
|