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 相关文章推荐
Python开发WebService系列教程之REST,web.py,eurasia,Django
Jun 30 Python
python实现telnet客户端的方法
Apr 15 Python
Python简单调用MySQL存储过程并获得返回值的方法
Jul 20 Python
Python的Django框架中forms表单类的使用方法详解
Jun 21 Python
python使用selenium实现批量文件下载
Mar 11 Python
django框架auth模块用法实例详解
Dec 10 Python
python爬虫添加请求头代码实例
Dec 28 Python
pytorch实现CNN卷积神经网络
Feb 19 Python
树莓派4B安装Tensorflow的方法步骤
Jul 16 Python
python实现最短路径的实例方法
Jul 19 Python
Python如何把字典写入到CSV文件的方法示例
Aug 23 Python
Python办公自动化之教你如何用Python将任意文件转为PDF格式
Jun 28 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
如何正确理解PHP的错误信息
2006/10/09 PHP
了解Joomla 这款来自国外的php网站管理系统
2010/03/11 PHP
PHP实现事件机制实例分析
2015/06/26 PHP
CI框架(CodeIgniter)实现的导入、导出数据操作示例
2018/05/24 PHP
php转换上传word文件为PDF的方法【基于COM组件】
2019/06/10 PHP
javascript 拖动表格行实现代码
2011/05/05 Javascript
jquery 年会抽奖程序
2011/12/22 Javascript
jQueryUI写一个调整分类的拖放效果实现代码
2012/05/10 Javascript
基于JS代码实现实时显示系统时间
2016/06/16 Javascript
jQuery实现输入框邮箱内容自动补全与上下翻动显示效果【附demo源码下载】
2016/09/20 Javascript
jquery 仿锚点跳转到页面指定位置的实例
2017/02/14 Javascript
浅谈AngularJS中使用$resource(已更新)
2017/09/14 Javascript
React Native使用fetch实现图片上传的示例代码
2018/03/07 Javascript
js replace 全局替换的操作方法
2018/06/12 Javascript
vue-router 手势滑动触发返回功能
2018/09/30 Javascript
jQuery实现滑动星星评分效果(每日分享)
2019/11/13 jQuery
jquery实现烟花效果(面向对象)
2020/03/10 jQuery
[01:07:13]TNC vs Pain 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
python调用windows api锁定计算机示例
2014/04/17 Python
安装Python的web.py框架并从hello world开始编程
2015/04/25 Python
Python面向对象之类的内置attr属性示例
2018/12/14 Python
python 判断字符串中是否含有汉字或非汉字的实例
2019/07/15 Python
基于Python检测动态物体颜色过程解析
2019/12/04 Python
python UIAutomator2使用超详细教程
2021/02/19 Python
HTML5 Canvas中使用路径描画二阶、三阶贝塞尔曲线
2015/01/01 HTML / CSS
英国的知名精品百货公司:House of Fraser(福来德)
2016/08/14 全球购物
英国玛莎百货美国官网:Marks & Spencer美国
2018/11/06 全球购物
将"引用"作为函数返回值类型的格式、好处和需要遵守的规则
2016/02/09 面试题
介绍一下Prototype的$()函数,$F()函数,$A()函数都是什么作用?
2014/03/05 面试题
个人承诺书格式
2014/06/03 职场文书
个人对照检查剖析材料
2014/10/13 职场文书
社会实践心得体会范文
2016/01/14 职场文书
建立共青团委员会的请示
2019/04/02 职场文书
python神经网络ResNet50模型
2022/05/06 Python
Mysql数据库group by原理详解
2022/07/07 MySQL
Python实现聚类K-means算法详解
2022/07/15 Python