Django实现学生管理系统


Posted in Python onFebruary 26, 2019

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
}

实现效果如下:

Django实现学生管理系统

Django实现学生管理系统

Django实现学生管理系统

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
MySQLdb ImportError: libmysqlclient.so.18解决方法
Aug 21 Python
Python实现检测服务器是否可以ping通的2种方法
Jan 01 Python
python关键字and和or用法实例
May 28 Python
实例解析Python设计模式编程之桥接模式的运用
Mar 02 Python
python的pip安装以及使用教程
Sep 18 Python
如何使用Python实现斐波那契数列
Jul 02 Python
浅析Python语言自带的数据结构有哪些
Aug 27 Python
Django实现网页分页功能
Oct 31 Python
Pycharm远程连接服务器并实现代码同步上传更新功能
Feb 25 Python
python tkinter GUI绘制,以及点击更新显示图片代码
Mar 14 Python
python+playwright微软自动化工具的使用
Feb 02 Python
关于python类SortedList详解
Sep 04 Python
python爬取微信公众号文章的方法
Feb 26 #Python
python下载微信公众号相关文章
Feb 26 #Python
python处理DICOM并计算三维模型体积
Feb 26 #Python
学习python可以干什么
Feb 26 #Python
Python3几个常见问题的处理方法
Feb 26 #Python
django 自定义过滤器的实现
Feb 26 #Python
使用Python将Mysql的查询数据导出到文件的方法
Feb 25 #Python
You might like
CakePHP框架Model关联对象用法分析
2017/08/04 PHP
PHP代码重构方法漫谈
2018/04/17 PHP
javascript+dom树型菜单类,希望朋友们一起进步
2007/05/03 Javascript
JQuery 解析多维的Json数据格式
2009/11/02 Javascript
jQuery插件jcrop+Fileapi完美实现图片上传+裁剪+预览的代码分享
2015/04/22 Javascript
jquery 构造函数在表单提交过程中修改数据
2015/05/25 Javascript
Vue实现购物车场景下的应用
2017/11/27 Javascript
canvas轨迹回放功能实现
2017/12/20 Javascript
详解angular部署到iis出现404解决方案
2018/08/14 Javascript
jQuery实现为动态添加的元素绑定事件实例分析
2018/09/07 jQuery
[02:18]DOTA2英雄基础教程 育母蜘蛛
2014/01/20 DOTA
解析Python编程中的包结构
2015/10/25 Python
Python打造出适合自己的定制化Eclipse IDE
2016/03/02 Python
Python随机数用法实例详解【基于random模块】
2017/04/18 Python
Python读取sqlite数据库文件的方法分析
2017/08/07 Python
python+matplotlib实现礼盒柱状图实例代码
2018/01/16 Python
python3爬取数据至mysql的方法
2018/06/26 Python
python实现弹窗祝福效果
2019/04/07 Python
解决python彩色螺旋线绘制引发的问题
2019/11/23 Python
基于python traceback实现异常的获取与处理
2019/12/13 Python
PyTorch中的padding(边缘填充)操作方式
2020/01/03 Python
matlab灰度图像调整及imadjust函数的用法详解
2020/02/27 Python
Python实现AI换脸功能
2020/04/10 Python
HTML5之消息通知的使用(Web Notification)
2018/10/30 HTML / CSS
澳大利亚免息网上购物:Shop Zero
2016/09/17 全球购物
OPPO手机官方商城:中国手机市场出货量第一品牌
2017/10/18 全球购物
京东港澳售:京东直邮港澳台
2018/01/31 全球购物
女装和独特珠宝:Sundance Catalog
2018/09/19 全球购物
水利局群众路线专题民主生活会发言材料
2014/09/21 职场文书
2014最新预备党员思想汇报范文:中国梦,我的梦
2014/10/25 职场文书
先进个人评语大全
2015/01/04 职场文书
公司经营目标责任书
2015/01/29 职场文书
毕业实习感受与体会
2015/05/26 职场文书
个人收入证明格式
2015/06/24 职场文书
小程序wx.getUserProfile接口的具体使用
2021/06/02 Javascript
Python写情书? 10行代码展示如何把情书写在她的照片里
2022/04/21 Python