python - Django 评论/优化/建议
问题描述
在过去的几周里,我一直在学习 Django,并且一直在从事一些项目作为实践和学习现实世界的应用程序。
我的页面(如下图所示)显示学生列表,以及分配给这些学生的教师,在这些页面上,我有许多表格可以做不同的事情,例如选择学生、创建新学生、编辑学生、分配教师等等
我在下面附上了我的代码,这一切都很好,但我不禁觉得这是一种效率极低的方法。热衷于听取人们的想法和反馈。我总是热衷于学习!
视图.py
@login_required(login_url='/login/')
def students(request):
if request.method == "POST":
## Checks which form is submitted, and actions the correct form
if 'new_student' in request.POST:
student_queryset = Student.objects.all().filter(creationuser__userschool=request.user.userschool)
all_teachers_queryset = Teacher.objects.all().filter(creationuser__userschool=request.user.userschool)
teacher_queryset = ''
search_term = ""
view_state = ''
new_teacher_state = ''
form = StudentForm(request.POST)
if form.is_valid():
save_form = form.save(commit=False)
save_form.creationuser = request.user
save_form.save()
save_form = StudentForm()
if 'new_teacher' in request.POST:
student_queryset = Student.objects.all().filter(creationuser__userschool=request.user.userschool)
all_teachers_queryset = Teacher.objects.all().filter(creationuser__userschool=request.user.userschool)
teacher_queryset = ''
search_term = ""
view_state = ''
new_teacher_state = ''
form = TeacherForm(request.POST)
if form.is_valid():
save_form = form.save(commit=False)
save_form.creationuser = request.user
save_form.save()
save_form = TeacherForm()
if 'assign_teacher' in request.POST:
student_queryset = Student.objects.all().filter(creationuser__userschool=request.user.userschool)
all_teachers_queryset = Teacher.objects.all().filter(creationuser__userschool=request.user.userschool)
teacher_queryset = ''
search_term = ""
view_state = ''
new_teacher_state = ''
form = TeacherForm()
selected = request.GET['select_student']
selected_student = get_object_or_404(Student, pk=selected)
teacher_selected = request.POST.getlist('assign_teacher')
teacher = get_object_or_404(Teacher, pk__in=teacher_selected)
selected_student.teachers.add(teacher)
selected_student.save()
if 'remove_teacher' in request.POST:
student_queryset = Student.objects.all().filter(creationuser__userschool=request.user.userschool)
all_teachers_queryset = Teacher.objects.all().filter(creationuser__userschool=request.user.userschool)
teacher_queryset = ''
search_term = ""
view_state = ''
new_teacher_state = ''
form = TeacherForm()
selected = request.GET['select_student']
selected_student = get_object_or_404(Student, pk=selected)
teacher_selected = request.POST.getlist('remove_teacher')
teacher = get_object_or_404(Teacher, pk__in=teacher_selected)
selected_student.teachers.remove(teacher)
selected_student.save()
elif 'delete_student' in request.POST:
form = StudentForm()
view_state = ''
new_teacher_state = ''
student_queryset = Student.objects.all().filter(creationuser__userschool=request.user.userschool)
all_teachers_queryset = Teacher.objects.all().filter(creationuser__userschool=request.user.userschool)
teacher_queryset = ''
search_term = ""
# Fetch list of items to delete, by ID
items_to_delete = request.POST.getlist('delete_student')
# Delete those items all in one go
Student.objects.filter(pk__in=items_to_delete).delete()
return redirect('./')
elif 'edit_student' in request.POST:
student_queryset = Student.objects.all().filter(creationuser__userschool=request.user.userschool)
all_teachers_queryset = Teacher.objects.all().filter(creationuser__userschool=request.user.userschool)
teacher_queryset = ''
search_term = ""
new_teacher_state = ''
selected = request.GET['select_student']
selected_student = Student.objects.all().filter(pk=selected)[0]
form = StudentForm(initial={'fullname': selected_student.fullname, 'firstname': selected_student.firstname, 'lastname': selected_student.lastname, 'homeschool': selected_student.homeschool, 'schoolclass': selected_student.schoolclass, 'yearlevel': selected_student.yearlevel, 'homeschoolday': selected_student.homeschoolday, 'weeklycommentday': selected_student.weeklycommentday})
view_state = 'edit'
elif 'new_teacher_view' in request.POST:
student_queryset = Student.objects.all().filter(creationuser__userschool=request.user.userschool)
view_state = ''
search_term = ""
selected = request.GET['select_student']
selected_student = Student.objects.all().filter(pk=selected)[0]
teacher_queryset = selected_student.teachers.all()
all_teachers_queryset = Teacher.objects.all().filter(creationuser__userschool=request.user.userschool).exclude(pk__in=teacher_queryset)
form = TeacherForm(initial={'school': selected_student.homeschool})
new_teacher_state = request.POST['new_teacher_view']
elif 'save_student' in request.POST:
student_queryset = Student.objects.all().filter(creationuser__userschool=request.user.userschool)
all_teachers_queryset = Teacher.objects.all().filter(creationuser__userschool=request.user.userschool)
teacher_queryset = ''
search_term = ""
new_teacher_state = ''
instance = Student.objects.get(pk=request.POST.get('save_student'))
form = StudentForm(request.POST, instance=instance)
if form.is_valid():
form.save()
view_state = ''
else:
view_state = 'edit'
elif 'search' in request.POST:
form = StudentForm()
view_state = ''
new_teacher_state = ''
search_term = request.POST['search']
student_queryset = Student.objects.all().filter(fullname__icontains=search_term, creationuser__userschool=request.user.userschool)
all_teachers_queryset = Teacher.objects.all().filter(creationuser__userschool=request.user.userschool)
teacher_queryset = ''
else:
form = StudentForm()
view_state = ''
new_teacher_state = ''
student_queryset = Student.objects.all().filter(creationuser__userschool=request.user.userschool)
all_teachers_queryset = Teacher.objects.all().filter(creationuser__userschool=request.user.userschool)
teacher_queryset = ''
search_term = ""
## Select Student Function
if 'select_student' in request.GET:
selected = request.GET['select_student']
selected_student = Student.objects.all().filter(pk=selected)[0]
teacher_queryset = selected_student.teachers.all()
else:
selected_student = ""
new = ""
## New Student Function
if 'new_student' in request.GET:
new = request.GET['new_student']
else:
new = ""
context = {
'students': student_queryset,
'search_text': search_term,
'form': form,
'selected_student': selected_student,
'new_student': new,
'view_state': view_state,
'new_teacher_view': new_teacher_state,
'teachers': teacher_queryset,
'all_teachers': all_teachers_queryset,
}
return render(request,"content_admin_pages/students.html",context)
学生.html
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block head %}
<style>
.student-list {
margin-left: 200px;
height: 100%;
width: 350px;
position: fixed;
overflow: hidden;
overflow-y: scroll;
border-right: 1px solid;
border-color: #d3d3d3;
}
.teacher-list {
margin-left: 550px;
height: 100%;
width: 100%;
overflow: hidden;
overflow-y: scroll;
border-right: 1px solid;
border-color: #d3d3d3;
}
.item-details {
margin-top: 10px;
margin-left: 15px;
padding: 0 0 0 0;
}
.student-div {
margin-left: 550px;
padding-top: 10px;
}
.student-div p {
padding-left: 20px;
}
.student-body {
margin-left: 550px;
padding-top: 10px;
padding-left: 20px;
}
.item-select-button {
position: absolute;
top: 20%;
right: 5px;
background: transparent;
border: none !important;
}
.new-teacher-button {
background: transparent;
border: none !important;
position: absolute;
right: 10px;
top: 23%;
}
.icon-only-button {
background: transparent;
border: none !important;
}
.search {
padding-left: 10px;
height: 40px;
width: 310px;
border: none;
padding-right: 2px;
}
.new_student_button {
height: 40px;
width: 30px;
background: transparent;
border: none !important;
padding: 0 0 0 0;
}
</style>
{% endblock %}
{% block content %}
<div class="student-list">
<div class="input-group" style="border-bottom: 1px solid;">
<form method="POST" name="search">
{% csrf_token %}
<input class="search" name="search" type="search" placeholder="Search.." value="{{ search_text }}">
</form>
<form method="GET" name="new_student">
<button type="submit" class="new_student_button" name="new_student" value="new">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" fill="currentColor" class="bi bi-plus-lg" viewBox="0 0 16 16"><path d="M8 0a1 1 0 0 1 1 1v6h6a1 1 0 1 1 0 2H9v6a1 1 0 1 1-2 0V9H1a1 1 0 0 1 0-2h6V1a1 1 0 0 1 1-1z"/></svg>
</button>
</form>
</div>
{% for instance in students %}
<div class="input-group" style="border-bottom: 1px solid; {% if selected_student.pk == instance.pk %} background: #0080ff; {% endif %}">
<div class="item-details">
<p {% if selected_student.pk == instance.pk %} style="color: white;" {% endif %}>
<b>{{ instance.fullname }}</b>
<br>
{{ instance.homeschool }}
</p>
</div>
<div class="select-item">
<form method="GET" class="form-group" name="select_student">
<button class="item-select-button" {% if selected_student.pk == instance.pk %} style="color: white;" {% endif %} type="submit" name="select_student" value="{{ instance.pk }}">
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" fill="currentColor" class="bi bi-arrow-right-short" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M4 8a.5.5 0 0 1 .5-.5h5.793L8.146 5.354a.5.5 0 1 1 .708-.708l3 3a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708-.708L10.293 8.5H4.5A.5.5 0 0 1 4 8z"/></svg>
</button>
</form>
</div>
</div>
{% endfor %}
</div>
{% if selected_student != "" %}
<div style="background-color: #EFEFEF; text-align:center; border-bottom: 1px solid;" class="student-div">
<h1>{{ selected_student.fullname }}</h1>
<form style="display:inline-block;" method="POST" class="form-group" name="delete_student">
{% csrf_token %}
<button onclick="return confirm('Are you sure you want to delete {{ selected_student.fullname }}?');" class="icon-only-button" type="submit" name="delete_student" value="{{ selected_student.pk }}">
<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16"><path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/><path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/></svg>
</button>
</form>
{% if view_state != "edit" %}
<form style="display:inline-block;" method="POST" class="form-group" name="edit_student">
{% csrf_token %}
<button class="icon-only-button form-group" type="submit" name="edit_student">
<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16"><path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/><path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/></svg>
</button>
</form>
{% endif %}
</div>
<div class="student-div">
{% if view_state != "edit" %}
<p><b>First Name:</b> {{selected_student.firstname}}</p>
<p><b>Last Name:</b> {{selected_student.lastname}}</p>
<p><b>Home School:</b> {{selected_student.homeschool}}</p>
<p><b>Year Level:</b> {{selected_student.yearlevel}}</p>
<p><b>Class:</b> {{selected_student.schoolclass}}</p>
<p><b>Home School Day:</b> {{selected_student.homeschoolday}}</p>
<p><b>Weekly Comment Day:</b> {{selected_student.weeklycommentday}}</p>
{% else %}
<form style="margin-left:20px;" method="POST" class="form-group" name="save_student">
{% csrf_token %}
{{ form|crispy }}
<br>
<button style="margin-left:45%;" type="submit" name="save_student" class="btn btn-success" value={{selected_student.pk}}>Save</button>
</form>
{% endif %}
</div>
<div style="background-color: #EFEFEF; text-align:center; position: relative; border-top: 1px solid; border-bottom: 1px solid;" class="student-div">
<h3>Teachers</h3>
<form method="POST" name="new_student_view">
{% csrf_token %}
{% if new_teacher_view == "" %}
<button class="new-teacher-button" type="submit" name="new_teacher_view" value="new">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" fill="currentColor" class="bi bi-person-plus-fill" viewBox="0 0 16 16"><path d="M1 14s-1 0-1-1 1-4 6-4 6 3 6 4-1 1-1 1H1zm5-6a3 3 0 1 0 0-6 3 3 0 0 0 0 6z"/><path fill-rule="evenodd" d="M13.5 5a.5.5 0 0 1 .5.5V7h1.5a.5.5 0 0 1 0 1H14v1.5a.5.5 0 0 1-1 0V8h-1.5a.5.5 0 0 1 0-1H13V5.5a.5.5 0 0 1 .5-.5z"/></svg>
</button>
<button style="right: 55px"class="new-teacher-button" type="submit" name="new_teacher_view" value="assign">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" fill="currentColor" class="bi bi-person-lines-fill" viewBox="0 0 16 16"><path d="M6 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm-5 6s-1 0-1-1 1-4 6-4 6 3 6 4-1 1-1 1H1zM11 3.5a.5.5 0 0 1 .5-.5h4a.5.5 0 0 1 0 1h-4a.5.5 0 0 1-.5-.5zm.5 2.5a.5.5 0 0 0 0 1h4a.5.5 0 0 0 0-1h-4zm2 3a.5.5 0 0 0 0 1h2a.5.5 0 0 0 0-1h-2zm0 3a.5.5 0 0 0 0 1h2a.5.5 0 0 0 0-1h-2z"/></svg>
</button>
{% else %}
<button class="new-teacher-button" type="submit" name="new_teacher_view" value="">
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" fill="currentColor" class="bi bi-x-lg" viewBox="0 0 16 16"><path d="M1.293 1.293a1 1 0 0 1 1.414 0L8 6.586l5.293-5.293a1 1 0 1 1 1.414 1.414L9.414 8l5.293 5.293a1 1 0 0 1-1.414 1.414L8 9.414l-5.293 5.293a1 1 0 0 1-1.414-1.414L6.586 8 1.293 2.707a1 1 0 0 1 0-1.414z"/></svg>
</button>
{% endif %}
</form>
</div>
{% if new_teacher_view == "" %}
<div class="teacher-list">
{% for instance in teachers %}
<div class="input-group" style="border-bottom: 1px solid;">
<form method="POST" class="form-group" name="remove_teacher">
{% csrf_token %}
<button class="icon-only-button" style="margin-top: 50%; margin-left: 10px;" onclick="return confirm('Are you sure you want to remove {{ instance.fullname }}?');" type="submit" name="remove_teacher" value={{instance.pk}}>
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-x-lg" viewBox="0 0 16 16"><path d="M1.293 1.293a1 1 0 0 1 1.414 0L8 6.586l5.293-5.293a1 1 0 1 1 1.414 1.414L9.414 8l5.293 5.293a1 1 0 0 1-1.414 1.414L8 9.414l-5.293 5.293a1 1 0 0 1-1.414-1.414L6.586 8 1.293 2.707a1 1 0 0 1 0-1.414z"/></svg>
</button>
</form>
<div class="item-details">
<p>
<b>{{ instance.fullname }}</b>
<br>
{{ instance.school }}
</p>
</div>
</div>
{% endfor %}
</div>
{% elif new_teacher_view == "new" %}
<div style="text-align: center" class="student-div">
<h3>Create a New Teacher</h3>
<form method="POST" class="form-group" name="new_teacher">
{% csrf_token %}
{{ form|crispy }}
<br>
<button type="submit" name="new_teacher" class="btn btn-success">Create</button>
</form>
</div>
</div>
{% else %}
<div class="teacher-list">
{% for instance in all_teachers %}
<div class="input-group" style="border-bottom: 1px solid;">
<form method="POST" class="form-group" name="assign_teacher">
{% csrf_token %}
<button class="icon-only-button" style="margin-top: 50%; margin-left: 10px;" type="submit" name="assign_teacher" value={{instance.pk}}>
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-plus-lg" viewBox="0 0 16 16"><path d="M8 0a1 1 0 0 1 1 1v6h6a1 1 0 1 1 0 2H9v6a1 1 0 1 1-2 0V9H1a1 1 0 0 1 0-2h6V1a1 1 0 0 1 1-1z"/></svg>
</button>
</form>
<div class="item-details">
<p>
<b>{{ instance.fullname }}</b>
<br>
{{ instance.school }}
</p>
</div>
</div>
{% endfor %}
</div>
{% endif %}
{% endif %}
{% if new_student == "new" %}
<div style="text-align: center" class="student-div">
<h3>Create a New Student</h3>
<form method="POST" class="form-group" name="student">
{% csrf_token %}
{{ form|crispy }}
<br>
<button type="submit" name="new_student" class="btn btn-success">Create</button>
</form>
</div>
</div>
{% endif %}
{% endblock %}
解决方案
您已经习惯了代码中的大量重复。将您的 if 语句作为一个函数,并使用所需的参数调用它并使用该值。而不是一次又一次地使用代码。
def check(request,Student,Teacher,pk,task):
if task in request.POST:
student_queryset = Student.objects.all().filter(creationuser__userschool=request.user.userschool)
all_teachers_queryset = Teacher.objects.all().filter(creationuser__userschool=request.user.userschool)
teacher_queryset = ''
search_term = ""
view_state = ''
new_teacher_state = ''
form = TeacherForm()
selected = request.GET['select_student']
selected_student = get_object_or_404(Student, pk=selected)
teacher_selected = request.POST.getlist('assign_teacher')
teacher = get_object_or_404(Teacher, pk__in=teacher_selected)
selected_student.teachers.add(teacher)
selected_student.save()
这只是一个例子,可能有错误,只是为了给你看。改变它你的需要
推荐阅读
- parallel-processing - 移动自动化 - Testng 非静态驱动程序 - TakeScreenshot 方法使用并行线程引发 NPE 错误
- python - 条件满足就发邮件
- android - 如何使用android中的多线程制作进度条以通过http范围标头表示块下载?
- c++ - Xcode:调试器在某些文件内的断点处停止,但不会在其他文件处停止
- git - git reset --hard HEAD~1 和 git revert HEAD 的异同是什么?
- react-navigation - SafeAreaView 不能在根目录下使用 MaterialTopTabNavigator
- image - Vue、TypeScript、多文件组件、图像未得到处理
- javascript - 将html转换为反应组件后JS无法正常工作
- webpack - Webpack - 无法读取未定义的属性“entryOption”
- unity3d - AddRelativeForce() 不相对加力