Django實(shí)現(xiàn)學(xué)生管理系統(tǒng)
Django學(xué)習(xí)筆記-學(xué)生管理系統(tǒng)(Django實(shí)現(xiàn))筆記中僅實(shí)現(xiàn)了對(duì)數(shù)據(jù)的全部查詢(xún)。
下面實(shí)現(xiàn)新增、刪除、修改,代碼如下。
下面的代碼沒(méi)有對(duì)輸入框內(nèi)容進(jìn)行限制,如果輸入不符合規(guī)則的內(nèi)容,會(huì)出現(xiàn)錯(cuò)誤。
本篇更新完畢后Django更新暫停一段,由于工作崗位是測(cè)試工程師,后面將重點(diǎn)關(guān)注測(cè)試相關(guān)內(nèi)容。
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>學(xué)生成績(jī)管理系統(tǒng)</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" >首頁(yè)</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" >學(xué) 號(hào):</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">數(shù)學(xué)成績(jī):</td>
<td style="border-style:none"><input name="math"></td>
</tr>
<tr>
<td style="border-style:none">語(yǔ)文成績(jī):</td>
<td style="border-style:none"><input name="chinese"></td>
</tr>
<tr>
<td style="border-style:none">英語(yǔ)成績(jī):</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 >學(xué)號(hào)</td>
<td >姓名</td>
<td >數(shù)學(xué)</td>
<td >語(yǔ)文</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>學(xué) 號(hào):<input name="id" type="text" value="{{ stu_detail.id}}" readonly="readonly" ></p>#}
{# <p>姓 名:<input name="name" type="text" value="{{ stu_detail.name}}"></p>#}
{# <p>數(shù)學(xué)成績(jī):<input name="math" type="text" value="{{ stu_detail.math}}"></p>#}
{# <p>語(yǔ)文成績(jī):<input name="chinese" type="text" value="{{ stu_detail.chinese}}"></p>#}
{# <p>英語(yǔ)成績(jī):<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" >學(xué) 號(hào):</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">數(shù)學(xué)成績(jī):</td>
<td style="border-style:none"><input name="math" type="text" value="{{ stu_detail.math}}"></td>
</tr>
<tr>
<td style="border-style:none">語(yǔ)文成績(jī):</td>
<td style="border-style:none"><input name="chinese" type="text" value="{{ stu_detail.chinese}}"></td>
</tr>
<tr>
<td style="border-style:none">英語(yǔ)成績(jī):</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 %}
靜態(tài)資源文件:
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
}
實(shí)現(xiàn)效果如下:



以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python3+django2開(kāi)發(fā)一個(gè)簡(jiǎn)單的人員管理系統(tǒng)過(guò)程詳解
- Django實(shí)現(xiàn)學(xué)員管理系統(tǒng)
- django認(rèn)證系統(tǒng)實(shí)現(xiàn)自定義權(quán)限管理的方法
- python3.6+django2.0開(kāi)發(fā)一套學(xué)員管理系統(tǒng)
- Django admin實(shí)現(xiàn)圖書(shū)管理系統(tǒng)菜鳥(niǎo)級(jí)教程完整實(shí)例
- 基于 Django 的手機(jī)管理系統(tǒng)實(shí)現(xiàn)過(guò)程詳解
相關(guān)文章
pytorch報(bào)錯(cuò)問(wèn)題:ValueError: num_samples should be
這篇文章主要介紹了pytorch報(bào)錯(cuò)問(wèn)題:ValueError: num_samples should be a positive integer value, but got num_samples=0的解決方案,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
關(guān)于DataFrame中某列值的替換map(dict)
這篇文章主要介紹了關(guān)于DataFrame中某列值的替換map(dict),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Python按天實(shí)現(xiàn)生成時(shí)間范圍序列的方法詳解
有的時(shí)候我們希望生成一段時(shí)間返回,比如從?2022-01-01?00:00:00?后面的?10?天,這么?10?個(gè)?datetime?對(duì)象,但是我們又不想自己去計(jì)算哪些月有30天哪些月有31天。所以本文將用Python實(shí)現(xiàn)按天自動(dòng)生成時(shí)間范圍序列,需要的可以參考一下2022-11-11
Python OS系統(tǒng)解決路徑中空格原因?qū)е挛募虿婚_(kāi)的問(wèn)題
這篇文章主要介紹了Python OS系統(tǒng)解決路徑中空格原因?qū)е挛募虿婚_(kāi)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
python使用pika庫(kù)調(diào)用rabbitmq參數(shù)使用詳情
這篇文章主要介紹了python使用pika庫(kù)調(diào)用rabbitmq參數(shù)使用詳情,文章通過(guò)展開(kāi)文章主題分享了三種方式,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
python如何導(dǎo)出微信公眾號(hào)文章方法詳解
這篇文章主要介紹了python如何導(dǎo)出微信公眾號(hào)文章方法詳解,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
利用OpenCV實(shí)現(xiàn)質(zhì)心跟蹤算法
質(zhì)心跟蹤算法不是正統(tǒng)的目標(biāo)跟蹤,而是在多目標(biāo)跟蹤中結(jié)合目標(biāo)檢測(cè)算法不同幀之間的相同目標(biāo)做一個(gè)link。本文將利用OpenCV實(shí)現(xiàn)質(zhì)心跟蹤算法,感興趣的可以試一試2022-01-01
Python實(shí)現(xiàn)兩種多分類(lèi)混淆矩陣
這篇文章主要為大家介紹了Python實(shí)現(xiàn)兩種多分類(lèi)混淆矩陣,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

