python终极篇 --- django---班级管理系统
2018-09-18 06:41:47来源:博客园 阅读 ()
周末没事自己写了个班级管理系统,虽然简单,但也算个前期学习的总结吧
from django.db import models # Create your models here. class Banji(models.Model): grade = models.CharField(max_length=32,unique=True) class Student(models.Model): name = models.CharField(max_length=32,unique=True) banji = models.ForeignKey(to="Banji",on_delete=models.CASCADE) class Teacher(models.Model): name = models.CharField(max_length=32,unique=True) banji = models.ManyToManyField("Banji")
"""class_crm URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), # 班级 url(r'^banji_list/', views.banji_list), url(r'^add_banji/', views.add_banji), url(r'^del_banji/', views.del_banji), url(r'^edit_banji/', views.edit_banji), url(r'^student_list/', views.student_list), url(r'^add_student/', views.add_student), url(r'^del_student/', views.del_student), url(r'^edit_student/', views.edit_student), url(r'^teacher_list/', views.teacher_list), url(r'^add_teacher/', views.add_teacher), url(r'^del_teacher/', views.del_teacher), url(r'^edit_teacher/', views.edit_teacher), ]
from django.shortcuts import render,HttpResponse,redirect from app01 import models # Create your views here. def banji_list(request): print(111) banji_list = models.Banji.objects.all().order_by("id") print(222) print(banji_list) return render(request,"banji_list.html",{"banji_list":banji_list}) def add_banji(request): if request.method == "POST": name = request.POST.get("new_name") if not name: return render(request,"add_banji.html",{"err_name":name,"err_msg":"不能为空"}) add_obj = models.Banji.objects.filter(grade=name) if add_obj: return render(request,"add_banji.html",{"err_name":name,"err_msg":"已存在"}) models.Banji.objects.create(grade=name) return redirect("/banji_list/") return render(request,"add_banji.html") def del_banji(request): del_id = request.GET.get("id") del_list = models.Banji.objects.filter(id=del_id) del_list.delete() return redirect("/banji_list/") def edit_banji(request): edit_id = request.GET.get("id") edit_obj = models.Banji.objects.get(id=edit_id) if request.method == "POST": name = request.POST.get("new_name") if not name: return render(request, "edit_banji.html", {"edit_obj": edit_obj,"err_msg":"不能为空"}) mod_obj = models.Banji.objects.filter(grade=name) if mod_obj: return render(request, "edit_banji.html", {"edit_obj": edit_obj,"err_msg":"重复了"}) if edit_obj and name and not mod_obj: edit_obj.grade = name edit_obj.save() return redirect("/banji_list/") if edit_obj: return render(request,"edit_banji.html",{"edit_obj":edit_obj}) return HttpResponse("不存在!!!") # 学生管理 def student_list(request): student_list = models.Student.objects.all().order_by("id") return render(request,"student_list.html",{"student_list":student_list}) def add_student(request): banji_list = models.Banji.objects.all() if request.method == "POST": name = request.POST.get("new_name") if not name: return render(request, "add_student.html", {"banji_list": banji_list,"err_name":name,"err_msg":"不能为空"}) add_obj = models.Student.objects.filter(name=name) if add_obj: return render(request, "add_student.html", {"banji_list": banji_list,"err_name":name,"err_msg":"此学生已存在"}) banji_id = request.POST.get("banji_id") models.Student.objects.create(name=name,banji_id=banji_id) return redirect("/student_list/") return render(request,"add_student.html",{"banji_list":banji_list}) def del_student(request): del_id = request.GET.get("id") del_obj = models.Student.objects.get(id=del_id) del_obj.delete() return redirect("/student_list/") def edit_student(request): edit_id = request.GET.get("id") edit_obj = models.Student.objects.filter(id=edit_id) banji_list = models.Banji.objects.all() if request.method == "POST": name = request.POST.get("new_name") banji_id = request.POST.get("banji_id") if not name: return render(request, "edit_student.html", {"edit_obj": edit_obj, "banji_list": banji_list,"err_msg":"不能为空"}) edit_obj = edit_obj[0] edit_obj.name = name edit_obj.banji_id = banji_id edit_obj.save() return redirect("/student_list/") return render(request,"edit_student.html",{"edit_obj":edit_obj[0],"banji_list":banji_list}) def teacher_list(request): teacher_list = models.Teacher.objects.all() return render(request,"teacher_list.html",{"teacher_list":teacher_list}) def add_teacher(request): banji_list = models.Banji.objects.all() if request.method == "POST": name = request.POST.get("new_name") add_obj = models.Teacher.objects.filter(name=name) if add_obj: return render(request, "add_teacher.html", {"banji_list": banji_list,"err_name":name,"err_msg":"已存在"}) banji_id = request.POST.getlist("banji_id") new_tea = models.Teacher.objects.create(name=name) new_tea.banji.set(banji_id) return redirect("/teacher_list/") return render(request,"add_teacher.html",{"banji_list":banji_list}) def del_teacher(request): del_id = request.GET.get("id") del_obj = models.Teacher.objects.filter(id=del_id) del_obj.delete() return redirect("/teacher_list/") def edit_teacher(request): edit_id = request.GET.get("id") edit_obj = models.Teacher.objects.filter(id=edit_id) banji_list = models.Banji.objects.all() if request.method == "POST": name = request.POST.get("new_name") banji_id = request.POST.getlist("banji_id") edit_obj = edit_obj[0] edit_obj.name = name edit_obj.save() edit_obj.banji.set(banji_id) return redirect("/teacher_list/") return render(request,"edit_teacher.html",{"banji_list":banji_list,"edit_obj":edit_obj[0]})
数据库表关系如下:
总结一些方法:
get为获取单个对象,找不到会报错,所以如果用的话最好try一下
filter获取的是query set集合,也可以理解为对象列表
getlist获取所有
HttpResponse('字符串') ——》 页面显示字符串的内容
render(request,'模板的名字',{k1:v1}) ——》 整个HTML文件返回给浏览器
redirect('/index/') 跳转 重定向 响应头 location:/index/
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash