首页 > 编程 > Python > 正文

Django实现学生管理系统

2020-02-16 01:21:48
字体:
来源:转载
供稿:网友

Django学习笔记-学生管理系统(Django实现)笔记中仅实现了对数据的全部查询。

下面实现新增、删除、修改,代码如下。

下面的代码没有对输入框内容进行限制,如果输入不符合规则的内容,会出现错误。

本篇更新完毕后Django更新暂停一段,由于工作岗位是测试工程师,后面将重点关注测试相关内容。

views.py

from django.shortcuts import render,reversefrom stusys import modelsfrom django.http import HttpResponseRedirect def stuinfo(request):  stuinfo_list_obj = models.Stuinfo.objects.all()  return render(request,'info.html',{'stuinfo_list':stuinfo_list_obj})def add_stuinfo(request):  if request.method == "POST":    id = request.POST['id']    name = request.POST['name']    math = request.POST['math']    chinese=request.POST['chinese']    english=request.POST['english']    total=float(math)+float(chinese)+float(english)    models.Stuinfo.objects.create(id=id,name=name,math=math,chinese=chinese,english=english,total=total)    return HttpResponseRedirect(reverse('stuinfo'))  elif request.method == "GET":    return render(request,'add.html') def del_stuinfo(request):  id=request.GET.get('id')  models.Stuinfo.objects.filter(id=id).delete()  return HttpResponseRedirect(reverse('stuinfo')) def mod_stuinfo(request):  if request.method=='GET':    id = request.GET.get('id')    stu_detail =models.Stuinfo.objects.get(id=id)    context={'stu_detail':stu_detail}    return render(request,'mod.html',context=context)  if request.method=="POST":    id = request.POST['id']    name = request.POST['name']    math = request.POST['math']    chinese=request.POST['chinese']    english=request.POST['english']    total=float(math)+float(chinese)+float(english)    models.Stuinfo.objects.filter(id=id).update(name=name,math=math,chinese=chinese,english=english,total=total)    return HttpResponseRedirect(reverse('stuinfo'))

urls.py

from django.contrib import adminfrom django.urls import pathfrom stusys import viewsurlpatterns = [  path('admin/', admin.site.urls),  path('',views.stuinfo,name='stuinfo'),  path('add/',views.add_stuinfo,name='add_stuinfo'),  path('del/',views.del_stuinfo,name='del_stuinfo'),  path('mod/',views.mod_stuinfo,name='mod_stuinfo')]

templates

base.html

{% load static %}<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>学生成绩管理系统</title>  <link rel="stylesheet" href="{% static 'nav.css' %}" rel="external nofollow" >  <link rel="stylesheet" href="{% static 'table.css' %}" rel="external nofollow" ></head><body>   <ul class="nav">        <li><a href="{% url 'stuinfo' %} " rel="external nofollow" >首页</a></li>        <li><a href="{% url 'add_stuinfo' %} " rel="external nofollow" >添加</a></li>  </ul>  <div style="padding:20px;margin-top:30px;background-color:#1abc9c;height:1500px;">    {% block content %} {% endblock %}  </div> </body></html>            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表