完整版学生管理系统: 1、用户模块 2、学生模块 3、带模板 4、多表操作(一对一,一对多) 5、用户权限验证
完整代码:https://github.com/luo981695830/--self/tree/master/day6
关于项目的整个流程可以参照django个人笔记(二)https://blog.csdn.net/luo981695830/article/details/99701341
重点说一下部分:
1、添加应用
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'user',
'student'
)
ps:常见问题,如果没有添加引用,数据库迁移同步的时候,不会生成迁移文件
2、主子路由分发
from django.conf.urls import include, url
from django.contrib import admin
from user.views import index
urlpatterns = [
# Examples:
# url(r'^$', 'day6.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$',index,name='index'),
url(r'^user/',include('user.urls',namespace='user')),
url(r'^student/',include('student.urls',namespace='student'))
]
3、用户模块路由分发
from django.conf.urls import include, url
from .views import user_register,user_logout,user_login
urlpatterns = [
url(r'user_register/$',user_register,name='user_register'),
url(r'user_login/$',user_login,name='user_login'),
url(r'user_logout/$',user_logout,name='user_logout'),
]
4、学生模块路由分发
from django.conf.urls import include, url
from django.contrib import admin
from .views import student_add,student_update,student_all,student_del
urlpatterns = [
url(r'^student_add/$',student_add,name='student_add'),
url(r'^student_update/(\d+)/$',student_update,name='student_update'),
url(r'^student_all/$',student_all,name='student_all'),
url(r'^student_del/(\d+)/$',student_del,name='student_del'),
]
5、学生模型
from django.db import models
# Create your models here.
# # 自定义模型管理器
# class StudentInfoManager(models.Manager):
# # 修改管理器返回的原始查询集合
# def all(self):
# # 重写all 方法,返回性别='girl'的学生
# return super(StudentInfoManager,self).all().filter(gender = 'girl')
#
# # 在模型类中自定义创建对象方法
# def create_object(self,name,age,gender):
# # 创建学生对象
# student = StudentInfo()
# # 获取数据
# student.name = name
# student.age = age
# student.gender = gender
# student.save()
# return student
class Banclass(models.Model):
'''班级信息'''
banname = models.CharField(max_length=20,verbose_name='班级名称')
def __str__(self):
return self.banname
class Meta:
verbose_name = '班级信息'
verbose_name_plural = verbose_name
class StudentInfo(models.Model):
'''学生信息'''
name = models.CharField(max_length=20,verbose_name='学生姓名')
age = models.IntegerField(verbose_name='学生年龄')
gender = models.CharField(choices=(('girl','女'),('boy','男')),default='girl',max_length=6,verbose_name='学生性别')
ban = models.ForeignKey(Banclass,verbose_name='所属班级')
def __str__(self):
return self.name
class Meta:
verbose_name = '学生信息'
verbose_name_plural = verbose_name
class StudentId(models.Model):
'''学号信息'''
stuid = models.CharField(max_length=20,verbose_name='学号')
student = models.OneToOneField(StudentInfo,verbose_name='所属学生')
def __str__(self):
return self.stuid
class Meta:
verbose_name = '学号信息'
verbose_name_plural = verbose_name
6、迁移同步
python manage.py makemigrations
python manage.py migrate
7、用户模块逻辑
from django.shortcuts import render,redirect
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate,login,logout
# Create your views here.
def index(request):
return render(request,'index.html')
def user_register(request):
if request.method == 'GET':
return render(request,'register.html')
else:
username = request.POST.get('username',None)
password = request.POST.get('password',None)
password1 = request.POST.get('password1',None)
user = User.objects.filter(username = username)
if user:
return render(request, 'register.html', {
'msg': '用户已存在'
})
else:
if password == password1:
user = User()
user.username = username
user.password = password
user.set_password(password)
user.save()
return redirect(reverse('user:user_login'))
else:
return render(request, 'register.html', {
'msg': '两次输入的密码不一致'
})
def user_login(request):
if request.method == 'GET':
return render(request,'login.html')
else:
username = request.POST.get('username',None)
password = request.POST.get('password',None)
# 如果这个地方使用authenticate方法验证 在注册逻辑中 必须使用set_password方法
a = authenticate(username=username,password=password)
if a:
login(request,a)
return redirect(reverse('index'))
else:
return render(request, 'login.html', {
'msg': '用户名或密码错误'
})
def user_logout(request):
logout(request)
return redirect(reverse('index'))
8、学生模块逻辑
from django.shortcuts import render,redirect
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from .models import StudentInfo,StudentId,Banclass
# Create your views here.
@login_required(login_url='/user/user_login/')
def student_all(request):
'''查看所有学生'''
all_students = StudentInfo.objects.all()
return render(request,'student_list.html',{
'all_students':all_students
})
@login_required(login_url='/user/user_login/')
def student_add(request):
'''添加学生'''
if request.method == 'GET':
return render(request,'student_add.html',{
'all_bans':Banclass.objects.all()
})
else:
stuname = request.POST.get('stuname', None)
stuage = request.POST.get('stuage', None)
stugender = request.POST.get('stugender', None)
stuban = request.POST.get('stuban', None)
stuuid = request.POST.get('stuuid', None)
student = StudentInfo()
student.name = stuname
student.age = stuage
student.gender = stugender
student.ban_id = Banclass.objects.filter(banname=stuban)[0].id
student.save()
studentid = StudentId()
studentid.student_id = student.id
studentid.stuid = stuuid
studentid.save()
return redirect(reverse('student:student_all'))
@login_required(login_url='/user/user_login/')
def student_del(request,stuid):
'''删除学生'''
student = StudentInfo.objects.filter(id = int(stuid))[0]
student.studentid.delete()
student.delete()
return redirect(reverse('student:student_all'))
@login_required(login_url='/user/user_login/')
def student_update(request,stuid):
'''修改学生'''
if request.method == 'GET':
student = StudentInfo.objects.filter(id = int(stuid))[0]
all_bans = Banclass.objects.all()
return render(request,'student_update.html',{
'student':student,
'all_bans':all_bans
})
else:
stuname = request.POST.get('stuname',None)
stuage = request.POST.get('stuage',None)
stugender = request.POST.get('stugender',None)
stuban = request.POST.get('stuban',None)
stuuid = request.POST.get('stuuid',None)
student = StudentInfo.objects.filter(id = int(stuid))[0]
student.name = stuname
student.age = stuage
student.gender = stugender
student.ban_id = Banclass.objects.filter(banname=stuban)[0].id
student.save()
studentid = student.studentid
studentid.student_id = student.id
studentid.stuid = stuuid
studentid.save()
return redirect(reverse('student:student_all'))