点击上方”python宝典”,关注获取python全套视频,
技术文章第一时间送达!
学生管理
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='root@live.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')
至此,学生的增改查就完成了。这里只是简单的实现,分页和删除参照上篇文章。
遇到问题欢迎加小编微信一起讨论
识别图中二维码,欢迎关注python宝典