/* 鼠标特效 */

python DJango 班级管理系统

DJango 班级管理系统

 

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")

models
"""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),
]

urls
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]})

Views

 

 

数据库表关系如下:

 

总结一些方法:

get为获取单个对象,找不到会报错,所以如果用的话最好try一下

filter获取的是query set集合,也可以理解为对象列表

getlist获取所有

HttpResponse('字符串')   ——》 页面显示字符串的内容
  render(request,'模板的名字',{k1:v1})  ——》 整个HTML文件返回给浏览器,字典作为值传入给HTML用
  redirect('/index/')    跳转  重定向  响应头  location:/index/

 

posted @ 2018-09-25 15:24  骚的刚刚好  阅读(1059)  评论(0编辑  收藏  举报
/* 鼠标特效 */