练习随笔 - django学生管理系统
Posted derrickwilliams
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习随笔 - django学生管理系统相关的知识,希望对你有一定的参考价值。
首先在models页面中创建表
其中一个学生表一个老师表
学生表关联老师表
from django.db import models
class Teacher(models.Model):
name = models.CharField(max_length=20,verbose_name=‘姓名‘)
username = models.CharField(max_length=20,verbose_name=‘登录名‘)
password = models.CharField(max_length=20,verbose_name=‘密码‘)
class Student(models.Model):
name = models.CharField(max_length=20,verbose_name=‘学生姓名‘)
age = models.IntegerField(verbose_name=‘年龄‘)
teacher = models.ForeignKey(Teacher,on_delete=models.SET_NULL,null=True,blank=True)
# Create your models here.
views.py页面
注释为解释
from django.shortcuts import render,HttpResponse,redirect
from . import models
def login(request):
#登录页面
if request.method == ‘GET‘:
return render(request, ‘login.html‘)
if request.method == ‘POST‘:
username = request.POST.get(‘username‘) #输入账号
password = request.POST.get(‘password‘) #输入密码
user_obj = models.Teacher.objects.filter(username=username,password=password).first()#数据库匹配
if user_obj: #判断是否有对应账户和密码是否匹配
request.session[‘user_id‘]=user_obj.id #存session
return redirect(‘/show/‘) #返回
else: #如果账户和密码不匹配
return HttpResponse(‘用户名或密码错误‘)
def register(request): #注册
if request.method == ‘GET‘:
return render(request,‘register.html‘)
if request.method == ‘POST‘:
name = request.POST.get(‘name‘) #输入名称
username = request.POST.get(‘username‘) #输入登录名
password = request.POST.get(‘password‘) #输入登录密码
re_pwd = request.POST.get(‘re_pwd‘) #重复提交密码
if username and password and re_pwd: #判断是否获取到三个值
if password == re_pwd: #如果两次输入密码一致
user_obj = models.Teacher.objects.filter(username=username).first() #查询用户名是否存在
if user_obj: #如果已经存在
return HttpResponse(‘用户已存在‘)
else: #如果都没有存在就将输入的存入数据库
models.Teacher.objects.create(name=name,username=username,password=password).save()
return redirect(‘/login/‘) #返回
else: #如果两次密码输入不一致
return HttpResponse(‘两次密码不一致‘)
else: #如果有空值
return HttpResponse(‘不能有空!‘)
def index(request): #展示所有老师
teacher = models.Teacher.objects.all() #数据库查询
return render(request, ‘index.html‘, locals()) #把变量传到html中
def show(request): #展示学生页
user_id = request.session.get(‘user_id‘) #取session
teacher = models.Teacher.objects.all().filter(id=user_id) #通过session查询登录的teacherid
context = {
‘teacher‘ : teacher #teacher传到Html
}
if request.method == ‘POST‘:
name = request.POST.get(‘name‘) #获得输入的name
age = request.POST.get(‘age‘) #获得输入的age
if name and age: #如果获取到了name和age
user_id = request.session.get(‘user_id‘) #取session
student = models.Student.objects.create(name=name,age=age,teacher_id=user_id) #添加学生的信息和关联的老师
else:
return HttpResponse(‘不能有空!‘)
return render(request, ‘show.html‘, context)
def student(request): #展示学生
user_id = request.session.get(‘user_id‘) #获取session
student = models.Student.objects.all().filter(teacher_id=user_id) #通过session查找到老师关联的学生
context = {
‘student‘ : student #传递到html
}
return render(request, ‘student.html‘, context)
def del_student(request,id): #删除
models.Student.objects.filter(id=id).update(teacher = None) #通过session查询学生的id然后将主键关联删除
return HttpResponse(‘删除成功‘)
def no_student(request): #没有分的学生
user_id = request.session.get(‘user_id‘) #取session
no_student = models.Student.objects.all().filter(teacher_id=None) #查询所有没有主键关联的学生
context = {
‘no_student‘ : no_student #传递到html
}
return render(request, ‘no_student.html‘, context)
def add_class(request,id): #添加班级
user_id = request.session.get(‘user_id‘) #取session
models.Student.objects.filter(id=id).update(teacher_id = user_id) #将没有关联的学生关联到老师
return HttpResponse(‘添加成功‘)
# Create your views here.
html页面
1. index页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页展示</title>
</head>
<body>
{% for i in teacher %}
{# 循环#}
{{ i.name }}
{# 取姓名#}
{% endfor %}
</body>
</html>
2.login页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<h1>登陆界面</h1>
{#最简单的html代码#}
{#这里要提醒下,提交到后台的get,post方式都可以,但是作为账户数据的话,用post#}
<form action="/login/" method="post">
{# form表单#}
{% csrf_token %}
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登陆">
</form>
</body>
</html>
3.no_student页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
还未分班的学生有:
<table>
<tr>
<td>学生</td>
<td>操作</td>
</tr>
<tr>
{% for x in no_student %}
<td>{{ x.name }}</td>
<td><a href="/add/{{ x.id }}">加入班级</a></td>
{# 切记前面加/#}
{% endfor %}
</tr>
</table>
</body>
</html>
4.register页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>register</title>
</head>
<body>
<h1>注册界面</h1>
<form action="" method="post">
{% csrf_token %}
姓名:<input type="text" name="name"><br>
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
确认密码:<input type="password" name="re_pwd"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
5.show页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页展示</title>
</head>
<body>
{% for i in teacher %}
欢迎你, {{ i.name }}
{% endfor %}
<form method="post">
{% csrf_token %}
学生姓名:<input type="text" name="name"><br>
年 龄:<input type="text" name="age"><br>
<button type="submit">提交</button>
</form>
<a href="/student/">点击查看您班级的学生</a>
<a href="/no_student/">点击查看未分班的学生</a>
</body>
</html>
6.student页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
您的学生有:
<table>
<tr>
<td>姓名</td>
<td>操作</td>
</tr>
{% for x in student %}
<tr>
<td>{{ x.name }}</td>
<td><a href="/del/{{ x.id }}/">删除</a></td>
</tr>
{% endfor %}
</table>
</body>
</html>
此练习为本人在某大某鸟的练习记录
如有不足望指正与理解
以上是关于练习随笔 - django学生管理系统的主要内容,如果未能解决你的问题,请参考以下文章