项目实战-后台管理系统(三)
程序员文章站
2022-06-01 22:42:10
...
学生管理
student.html
{% extends "base.html" %}
{% block css %}
{% endblock %}
{% block content %}
<h1>学生列表</h1>
<div>
<a href="/add_student.html">添加</a>
</div>
<table border="1">
<thead>
<tr>
<th>学生ID</th>
<th>学生姓名</th>
<th>学生邮箱</th>
<th>所属班级ID</th>
<th>所属班级</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for row in result %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.name }}</td>
<td>{{ row.email }}</td>
<td>{{ row.cls.id }}</td>
<td>{{ row.cls.caption }}</td>
<td>
<a href="/edit_student.html?nid={{ row.id }}">编辑</a> | <a>删除</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
{% block js %}
<script>
$(function () {
$('#menu_student').addClass('active');
})
</script>
{% endblock %}
add_student.html
{% extends "base.html" %}
{% block css %}
{% endblock %}
{% block content %}
<h1>创建学生</h1>
<form action="/add_student.html" method="POST">
<p>
<input placeholder="学生姓名" type="text" name="name" />
</p>
<p>
<input placeholder="学生邮箱" type="text" name="email" />
</p>
<p>
<!-- <input placeholder="班级ID" type="text" name="cls_id" /> -->
<select name="cls_id">
{% for op in cls_list %}
<option value="{{ op.id }}">{{ op.caption }}</option>
{% endfor %}
</select>
</p>
<input type="submit" value="提交"/>
</form>
{% endblock %}
{% block js %}
<script>
$(function () {
$('#menu_student').addClass('active');
});
</script>
{% endblock %}
edit_student.html
{% extends "base.html" %}
{% block css %}
{% endblock %}
{% block content %}
<h1>编辑学生</h1>
<form action="/edit_student.html" method="POST">
<input class="hide" type="text" name="id" value="{{ obj.id }}" />
<p>
<input placeholder="学生姓名" type="text" name="name" value="{{ obj.name }}" />
</p>
<p>
<input placeholder="学生邮箱" type="text" name="email" value="{{ obj.email }}" />
</p>
<p>
<!-- <input placeholder="班级ID" type="text" name="cls_id" /> -->
<select name="cls_id">
{% for op in cls_list %}
{% if op.id == obj.cls_id %}
<option selected="selected" value="{{ op.id }}">{{ op.caption }}</option>
{% else %}
<option value="{{ op.id }}">{{ op.caption }}</option>
{% endif %}
{% endfor %}
</select>
</p>
<input type="submit" value="提交"/>
</form>
{% endblock %}
{% block js %}
<script>
$(function () {
$('#menu_student').addClass('active');
});
</script>
{% endblock %}
urls.py
from django.contrib import admin
from django.urls import path,re_path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login.html', views.login),
path('index.html', views.index),
path('logout.html', views.logout),
path('classes.html', views.handle_classes),
path('add_classes.html', views.handle_add_classes),
path('edit_classes.html', views.handle_edit_classes),
path('del_classes.html', views.handle_del_classes),
path('student.html$', views.handle_student),
path('add_student.html$', views.add_student),
path('edit_student.html$', views.edit_student),
]
veiws.py
@auth
def handle_student(request):
if request.method == "GET":
# for i in range(10):
# models.Student.objects.create(name='root' + str(i),
# email='aaa@qq.com' + str(i),
# cls_id=i)
result = models.Student.objects.all()
current_user = request.session.get('username')
return render(request, 'student.html', {'username': current_user,'result': result})
elif request.method == "POST":
return redirect('/index.html')
else:
return redirect('/index.html')
@auth
def add_student(request):
if request.method == "GET":
return render(request, 'add_student.html')
elif request.method == "POST":
name = request.POST.get('name')
email = request.POST.get('email')
cls_id = request.POST.get('cls_id')
models.Student.objects.create(name=name,email=email,cls_id=cls_id)
return redirect('/student.html')
@auth
def edit_student(request):
if request.method == "GET":
cls_list = models.Classes.objects.all()[0: 20]
nid = request.GET.get('nid')
obj = models.Student.objects.get(id=nid)
return render(request, 'edit_student.html', {'cls_list': cls_list, "obj": obj})
elif request.method == "POST":
nid = request.POST.get('id')
name = request.POST.get('name')
email = request.POST.get('email')
cls_id = request.POST.get('cls_id')
models.Student.objects.filter(id=nid).update(name=name,email=email,cls_id=cls_id)
return redirect('/student.html')
至此,学生的增改查就完成了。这里只是简单的实现,分页和删除参照上篇文章。
遇到问题欢迎加小编微信一起讨论
识别图中二维码,领取pyhton全套视频资料
下一篇: 键盘常见故障与维修技巧
推荐阅读
-
Django后台管理系统的图文使用教学
-
有美食餐饮管理系统后台连接打印机的设置方法
-
ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统之前端页面框架构建源码分享
-
python实现名片管理系统项目
-
【SSH网上商城项目实战29】使用JsChart技术在后台显示商品销售报表
-
详解使用VUE搭建后台管理系统(vue-cli更新至3.0)
-
MVC+EasyUI+三层架构简单权限管理系统
-
.NET Core实战项目之CMS 第八章 设计篇-内容管理极简设计全过程
-
oa项目描述介绍(oa系统项目管理方案)
-
.NET Core实战项目之CMS 第十章 设计篇-系统开发框架设计