Django实现学生管理系统
作者:yaoliuwei1426 发布时间:2023-07-22 18:05:59
标签:Django,管理系统
Django学习笔记-学生管理系统(Django实现)笔记中仅实现了对数据的全部查询。
下面实现新增、删除、修改,代码如下。
下面的代码没有对输入框内容进行限制,如果输入不符合规则的内容,会出现错误。
本篇更新完毕后Django更新暂停一段,由于工作岗位是测试工程师,后面将重点关注测试相关内容。
views.py
from django.shortcuts import render,reverse
from stusys import models
from 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 admin
from django.urls import path
from stusys import views
urlpatterns = [
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>
add.html
{% extends 'base.html' %}
{% block content %}
<div>
<form action ="{% url 'add_stuinfo' %}" method="post">
{% csrf_token %}
<table class="table" style="border-style:none;width: 50%" >
<tr>
<td style="border-style:none" >学 号:</td>
<td style="border-style:none"><input name="id"></td>
</tr>
<tr>
<td style="border-style:none">姓 名:</td>
<td style="border-style:none"><input name="name"></td>
</tr>
<tr>
<td style="border-style:none">数学成绩:</td>
<td style="border-style:none"><input name="math"></td>
</tr>
<tr>
<td style="border-style:none">语文成绩:</td>
<td style="border-style:none"><input name="chinese"></td>
</tr>
<tr>
<td style="border-style:none">英语成绩:</td>
<td style="border-style:none"><input name="english"></td>
</tr>
<tr>
<td colspan="2" style="border-style:none" ><input type="submit" value="添加" style="width:100px;height:40px;"></td>
</tr>
</table>
</form>
</div>
{% endblock %}
info.html
{% extends 'base.html' %}
{% block content %}
<table class="table" >
<thead>
<tr >
<td >学号</td>
<td >姓名</td>
<td >数学</td>
<td >语文</td>
<td >英文</td>
<td >总分</td>
<td colspan="2"> </td>
</tr>
</thead>
<tbody>
{% for stuinfo in stuinfo_list %}
<tr >
<td >{{ stuinfo.id }}</td>
<td >{{ stuinfo.name }}</td>
<td >{{ stuinfo.math}}</td>
<td >{{ stuinfo.chinese }}</td>
<td >{{ stuinfo.english }}</td>
<td >{{ stuinfo.total }}</td>
<td ><a href="{% url 'del_stuinfo' %}?id={{ stuinfo.id}}" rel="external nofollow" >删除</a></td>
<td ><a href="{% url 'mod_stuinfo' %}?id={{ stuinfo.id}}" rel="external nofollow" >修改</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
mod.html
{% extends 'base.html' %}
{% block content %}
{# <form action ="{% url 'mod_stuinfo' %}" method="post">#}
{# {% csrf_token %}#}
{# #}
{# <p>学 号:<input name="id" type="text" value="{{ stu_detail.id}}" readonly="readonly" ></p>#}
{# <p>姓 名:<input name="name" type="text" value="{{ stu_detail.name}}"></p>#}
{# <p>数学成绩:<input name="math" type="text" value="{{ stu_detail.math}}"></p>#}
{# <p>语文成绩:<input name="chinese" type="text" value="{{ stu_detail.chinese}}"></p>#}
{# <p>英语成绩:<input name="english" type="text" value="{{ stu_detail.english}}"></p>#}
{# <p><input type="submit" value="修改"></p>#}
{# </form>#}
<form action ="{% url 'mod_stuinfo' %}" method="post">
{% csrf_token %}
<table class="table" style="border-style:none;width: 50%" >
<tr>
<td style="border-style:none" >学 号:</td>
<td style="border-style:none"><input name="id" type="text" value="{{ stu_detail.id}}" readonly="readonly" disabled="disabled"></td>
</tr>
<tr>
<td style="border-style:none">姓 名:</td>
<td style="border-style:none"><input name="name" type="text" value="{{ stu_detail.name}}"></td>
</tr>
<tr>
<td style="border-style:none">数学成绩:</td>
<td style="border-style:none"><input name="math" type="text" value="{{ stu_detail.math}}"></td>
</tr>
<tr>
<td style="border-style:none">语文成绩:</td>
<td style="border-style:none"><input name="chinese" type="text" value="{{ stu_detail.chinese}}"></td>
</tr>
<tr>
<td style="border-style:none">英语成绩:</td>
<td style="border-style:none"><input name="english" type="text" value="{{ stu_detail.english}}"></td>
</tr>
<tr>
<td colspan="2" style="border-style:none" ><input type="submit" value="修改" style="width:100px;height:40px;"></td>
</tr>
</table>
</form>
{% endblock %}
静态资源文件:
nav.css
*{
margin: 0;
padding: 0;
}
.nav{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
top: 0;
width: 100%;
}
.nav li{
float: left;
}
.nav li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.nav li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #4CAF50;
}
table.css
.table{
margin-top:50px;width:100% ;border:solid #add9c0; border-width:1px 0px 0px 1px;}
.table tr td {
border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;font-size:18px;align:center;}
.table tr td input{
width: 250px; height: 30px;font-size:18px
}
实现效果如下:
来源:https://blog.csdn.net/yaoliuwei1426/article/details/82854539
0
投稿
猜你喜欢
- 图像标注在计算机视觉中很重要,计算机视觉是一种技术,它允许计算机从数字图像或视频中获得高水平的理解力,并以人类的方式观察和解释视觉信息。注释
- 首先我们解压$ unzip p8202632_10205_LINUX.zip解压后我们会发现多出了个文件夹,他是:Disk1,进入Disk1
- 所谓线性最小二乘法,可以理解为是解方程的延续,区别在于,当未知量远小于方程数的时候,将得到一个无解的问题。最小二乘法的实质,是保证误差最小的
- 有时候我们没办法得到pdf或者word文档,这个时候会使用手机或者相机进行拍照,往往会出现背景,打印出来就是灰色的或者有黑色的背景,这个时候
- 前言:这个先来创建一个模块,名称为christmastree,在该模块中,首先定义一个全局变量,然后创建一个名称为fun_christmas
- Pyplotmatplotlib.pyplot是一个命令型函数集合,它可以让我们像使用MATLAB一样使用matplotlib。pyplot
- 本文实例讲述了Flask框架学习笔记之表单基础介绍与表单提交方式。分享给大家供大家参考,具体如下:表单介绍表单是HTML页面中负责数据采集功
- 阅读上一篇:AJAX的jQuery实现入门(一)要写入数据库,我们知道的最简单的就是注册了, 就做个最简单的注册表单, 看看是如何提交数据的
- Python用Pillow(PIL)进行简单的图像操作方法颜色与RGBA值计算机通常将图像表示为RGB值,或者再加上alpha值(通透度,透
- np.newaxis 新增一个轴 如何将数组[0,1,2]转换成列向量用ndarray[: , np.newaxis]代码实质就是将原本的(
- $array=explode(separator,$string); $string=implode(glue,$array);使用和理解这
- 函数的必选参数,指的是函数调用的时候必须传入的参数import mathdef cal (n): return n * nvar
- 前提条件:1.安装好Wampserver64(版本不限)2.Wampserver64软件启动后 变为绿色如:3.在数据库里面创建好名为&am
- python装饰器就是用于扩展原函数功能的一种函数,这个函数特殊的地方就是它的返回值也是一个函数,使用Python装饰器的一个好处就是:在不
- 在PCA中有遇到,在这里记录一下计算矩阵的特征值个特征向量,下面给出几个示例代码:在使用前需要单独import一下>>>
- 一 方法汇总在 Python 进程中,有几种方法可以实现数据交互:共享内存:这是一种用于进程间通信的高效方式。多个进程可以访问同一个共享内存
- 上节我们了解了图形验证码的识别,简单的图形验证码我们可以直接利用 Tesserocr 来识别,但是近几年又出现了一些新型验证码,如滑动验证码
- 私有变量表示方法在变量前加上两个下划线的是私有变量。class Teacher(): def __init__(self,nam
- 本文讲述了Python检测网络延迟的代码。分享给大家供大家参考,具体如下:#!/usr/bin/env python # coding: u
- 环境OpenCV3.4.16(C++)opencv-contrib-python 4.5.4.60(Python)验证Opencv函数fil