django学习_学生信息管理后台
一、功能:
- 实现对学生对个人信息的增删查改
- 实现后台对所有学生信息的操作
二、此后台采用的是MVC的风格模式进行编码
1.VIEWS部分
from django.http import HttpResponse from django.shortcuts import render from .models import Student, OneClass # from django.http import request # Create your views here. def index(request): return render(request, 'index.html') def add(request): name = request.GET.get('name') age = request.GET.get('age') learn = request.GET.get('learn') type1 = request.GET.get('type1') # type 为1表示执行数据库操作,否则只是跳转页面 if type1 == '1': Student.objects.create(name=name, age=age, learn=learn) # 在student 表里新建一条记录 # return render(request, 'add.html',) return render(request, 'add.html') def delete(request): name = request.GET.get('name') type1 = request.GET.get('type1') # type 为 1 表示执行数据库查询操作 # if type1 == '1': # # 查询所在记录 # try: # stu: Student = Student.objects.filter(name=name).get() # except Exception: # return HttpResponse("无此记录") # return render(request, 'delete.html', {'stu': stu}) # type 为 2 表示执行数据库确认删除操作 if type1 == '2': try: Student.objects.filter(name=name).get() except Exception: return HttpResponse("无此记录") Student.objects.filter(name=name).delete() # 按照姓名, 进行过滤 return render(request, 'delete.html', {'msg': '删除成功'}) return render(request, 'delete.html') def update(request): name = request.GET.get('name') age = request.GET.get('age') learn = request.GET.get('learn') type1 = request.GET.get('type1') # type 为1表示执行数据库操作,否则只是跳转页面 if type1 == '1': try: stu = Student.objects.get(name=name) stu.name = name stu.age = age stu.learn = learn stu.save() return render(request, "update.html", {'msg': '修改成功'}) except Exception: return HttpResponse("无此记录") return render(request, 'update.html') def query(request): name = request.GET.get('name') type1 = request.GET.get('type1') # type 为1表示执行数据库操作,否则只是跳转页面 if type1 == '1': if name == 'all': stu_list = Student.objects.all() return render(request, 'query.html', {'stu_list': stu_list}) try: Student.objects.filter(name=name).get() except Exception: return HttpResponse("无此记录") stu = Student.objects.get(name=name) # 在student 表里查询一条记录 return render(request, 'query.html', {'stu': stu}) return render(request, 'query.html')
2.MODELS 部分
from django.db import models # Create your models here. # 学生类模板 class Student(models.Model): name = models.CharField(max_length=20) age = models.IntegerField() learn = models.CharField(max_length=20) # 教师类模板 class Teacher(models.Model): name = models.CharField(max_length=20) age = models.IntegerField() teach = models.CharField(max_length=20) student = models.ManyToManyField(Student, through="OneClass") # 班级类模板 class OneClass(models.Model): name = models.CharField(max_length=50) teacher = models.ForeignKey('Teacher', on_delete=models.CASCADE) student = models.ForeignKey('Student', on_delete=models.CASCADE)
3.CONTROLLER部分
- URL配置
from django.conf.urls import url from . import views urlpatterns=[ url(r'^index', views.index), url(r'^add', views.add), url(r'^delete', views.delete), url(r'^query', views.query), url(r'^update', views.update) ]
五、功能展示
1.主界面(功能选择)
2.增加学生信息
3.查询学生信息
4.删除学生信息
作者:含笑半步颠√
博客链接:https://www.cnblogs.com/lixy-88428977
声明:本文为博主学习感悟总结,水平有限,如果不当,欢迎指正。如果您认为还不错,欢迎转载。转载与引用请注明作者及出处。
引用"虫师":生活依旧,工作依旧,学习依旧,趁着对技术还热情,继续学习与总结。