当前位置:   article > 正文

django个人笔记(三)—— 完整版学生管理系统_django框架开发智慧星学生管理系统

django框架开发智慧星学生管理系统
完整版学生管理系统:
1、用户模块
2、学生模块
3、带模板
4、多表操作(一对一,一对多)
5、用户权限验证

完整代码:https://github.com/luo981695830/--self/tree/master/day6

 

关于项目的整个流程可以参照django个人笔记(二)https://blog.csdn.net/luo981695830/article/details/99701341

重点说一下部分:

1、添加应用

  1. INSTALLED_APPS = (
  2. 'django.contrib.admin',
  3. 'django.contrib.auth',
  4. 'django.contrib.contenttypes',
  5. 'django.contrib.sessions',
  6. 'django.contrib.messages',
  7. 'django.contrib.staticfiles',
  8. 'user',
  9. 'student'
  10. )

ps:常见问题,如果没有添加引用,数据库迁移同步的时候,不会生成迁移文件

2、主子路由分发

  1. from django.conf.urls import include, url
  2. from django.contrib import admin
  3. from user.views import index
  4. urlpatterns = [
  5. # Examples:
  6. # url(r'^$', 'day6.views.home', name='home'),
  7. # url(r'^blog/', include('blog.urls')),
  8. url(r'^admin/', include(admin.site.urls)),
  9. url(r'^$',index,name='index'),
  10. url(r'^user/',include('user.urls',namespace='user')),
  11. url(r'^student/',include('student.urls',namespace='student'))
  12. ]

3、用户模块路由分发

  1. from django.conf.urls import include, url
  2. from .views import user_register,user_logout,user_login
  3. urlpatterns = [
  4. url(r'user_register/$',user_register,name='user_register'),
  5. url(r'user_login/$',user_login,name='user_login'),
  6. url(r'user_logout/$',user_logout,name='user_logout'),
  7. ]

4、学生模块路由分发

  1. from django.conf.urls import include, url
  2. from django.contrib import admin
  3. from .views import student_add,student_update,student_all,student_del
  4. urlpatterns = [
  5. url(r'^student_add/$',student_add,name='student_add'),
  6. url(r'^student_update/(\d+)/$',student_update,name='student_update'),
  7. url(r'^student_all/$',student_all,name='student_all'),
  8. url(r'^student_del/(\d+)/$',student_del,name='student_del'),
  9. ]

5、学生模型

  1. from django.db import models
  2. # Create your models here.
  3. # # 自定义模型管理器
  4. # class StudentInfoManager(models.Manager):
  5. # # 修改管理器返回的原始查询集合
  6. # def all(self):
  7. # # 重写all 方法,返回性别='girl'的学生
  8. # return super(StudentInfoManager,self).all().filter(gender = 'girl')
  9. #
  10. # # 在模型类中自定义创建对象方法
  11. # def create_object(self,name,age,gender):
  12. # # 创建学生对象
  13. # student = StudentInfo()
  14. # # 获取数据
  15. # student.name = name
  16. # student.age = age
  17. # student.gender = gender
  18. # student.save()
  19. # return student
  20. class Banclass(models.Model):
  21. '''班级信息'''
  22. banname = models.CharField(max_length=20,verbose_name='班级名称')
  23. def __str__(self):
  24. return self.banname
  25. class Meta:
  26. verbose_name = '班级信息'
  27. verbose_name_plural = verbose_name
  28. class StudentInfo(models.Model):
  29. '''学生信息'''
  30. name = models.CharField(max_length=20,verbose_name='学生姓名')
  31. age = models.IntegerField(verbose_name='学生年龄')
  32. gender = models.CharField(choices=(('girl','女'),('boy','男')),default='girl',max_length=6,verbose_name='学生性别')
  33. ban = models.ForeignKey(Banclass,verbose_name='所属班级')
  34. def __str__(self):
  35. return self.name
  36. class Meta:
  37. verbose_name = '学生信息'
  38. verbose_name_plural = verbose_name
  39. class StudentId(models.Model):
  40. '''学号信息'''
  41. stuid = models.CharField(max_length=20,verbose_name='学号')
  42. student = models.OneToOneField(StudentInfo,verbose_name='所属学生')
  43. def __str__(self):
  44. return self.stuid
  45. class Meta:
  46. verbose_name = '学号信息'
  47. verbose_name_plural = verbose_name

6、迁移同步

python manage.py makemigrations

python manage.py migrate

 

7、用户模块逻辑

  1. from django.shortcuts import render,redirect
  2. from django.core.urlresolvers import reverse
  3. from django.contrib.auth.models import User
  4. from django.contrib.auth import authenticate,login,logout
  5. # Create your views here.
  6. def index(request):
  7. return render(request,'index.html')
  8. def user_register(request):
  9. if request.method == 'GET':
  10. return render(request,'register.html')
  11. else:
  12. username = request.POST.get('username',None)
  13. password = request.POST.get('password',None)
  14. password1 = request.POST.get('password1',None)
  15. user = User.objects.filter(username = username)
  16. if user:
  17. return render(request, 'register.html', {
  18. 'msg': '用户已存在'
  19. })
  20. else:
  21. if password == password1:
  22. user = User()
  23. user.username = username
  24. user.password = password
  25. user.set_password(password)
  26. user.save()
  27. return redirect(reverse('user:user_login'))
  28. else:
  29. return render(request, 'register.html', {
  30. 'msg': '两次输入的密码不一致'
  31. })
  32. def user_login(request):
  33. if request.method == 'GET':
  34. return render(request,'login.html')
  35. else:
  36. username = request.POST.get('username',None)
  37. password = request.POST.get('password',None)
  38. # 如果这个地方使用authenticate方法验证 在注册逻辑中 必须使用set_password方法
  39. a = authenticate(username=username,password=password)
  40. if a:
  41. login(request,a)
  42. return redirect(reverse('index'))
  43. else:
  44. return render(request, 'login.html', {
  45. 'msg': '用户名或密码错误'
  46. })
  47. def user_logout(request):
  48. logout(request)
  49. return redirect(reverse('index'))

 8、学生模块逻辑

  1. from django.shortcuts import render,redirect
  2. from django.core.urlresolvers import reverse
  3. from django.contrib.auth.decorators import login_required
  4. from .models import StudentInfo,StudentId,Banclass
  5. # Create your views here.
  6. @login_required(login_url='/user/user_login/')
  7. def student_all(request):
  8. '''查看所有学生'''
  9. all_students = StudentInfo.objects.all()
  10. return render(request,'student_list.html',{
  11. 'all_students':all_students
  12. })
  13. @login_required(login_url='/user/user_login/')
  14. def student_add(request):
  15. '''添加学生'''
  16. if request.method == 'GET':
  17. return render(request,'student_add.html',{
  18. 'all_bans':Banclass.objects.all()
  19. })
  20. else:
  21. stuname = request.POST.get('stuname', None)
  22. stuage = request.POST.get('stuage', None)
  23. stugender = request.POST.get('stugender', None)
  24. stuban = request.POST.get('stuban', None)
  25. stuuid = request.POST.get('stuuid', None)
  26. student = StudentInfo()
  27. student.name = stuname
  28. student.age = stuage
  29. student.gender = stugender
  30. student.ban_id = Banclass.objects.filter(banname=stuban)[0].id
  31. student.save()
  32. studentid = StudentId()
  33. studentid.student_id = student.id
  34. studentid.stuid = stuuid
  35. studentid.save()
  36. return redirect(reverse('student:student_all'))
  37. @login_required(login_url='/user/user_login/')
  38. def student_del(request,stuid):
  39. '''删除学生'''
  40. student = StudentInfo.objects.filter(id = int(stuid))[0]
  41. student.studentid.delete()
  42. student.delete()
  43. return redirect(reverse('student:student_all'))
  44. @login_required(login_url='/user/user_login/')
  45. def student_update(request,stuid):
  46. '''修改学生'''
  47. if request.method == 'GET':
  48. student = StudentInfo.objects.filter(id = int(stuid))[0]
  49. all_bans = Banclass.objects.all()
  50. return render(request,'student_update.html',{
  51. 'student':student,
  52. 'all_bans':all_bans
  53. })
  54. else:
  55. stuname = request.POST.get('stuname',None)
  56. stuage = request.POST.get('stuage',None)
  57. stugender = request.POST.get('stugender',None)
  58. stuban = request.POST.get('stuban',None)
  59. stuuid = request.POST.get('stuuid',None)
  60. student = StudentInfo.objects.filter(id = int(stuid))[0]
  61. student.name = stuname
  62. student.age = stuage
  63. student.gender = stugender
  64. student.ban_id = Banclass.objects.filter(banname=stuban)[0].id
  65. student.save()
  66. studentid = student.studentid
  67. studentid.student_id = student.id
  68. studentid.stuid = stuuid
  69. studentid.save()
  70. return redirect(reverse('student:student_all'))

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/154075
推荐阅读
相关标签
  

闽ICP备14008679号