文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

python终极篇 --- django

2023-01-30 22:16

关注

周末没事自己写了个班级管理系统,虽然简单,但也算个前期学习的总结吧

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文件返回给浏览器
  redirect('/index/')    跳转  重定向  响应头  location:/index/

 

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯