【发布时间】:2021-09-19 09:46:16
【问题描述】:
我正在开发一个在线学校系统,我对 Django 很陌生。
我在我的数据库中创建了一个学生列表,并从该数据库中将学生数据提供给我的模板并将其显示在一个表格中。
现在我想确认它们是否存在于课堂中,并将该数据返回给后端。我有一个选择选项来选择是或否。然后我有一个提交按钮。但是提交后,我只得到了最后一个学生的数据。
请说明如何将所有学生数据获取到后端。
我的模型
class StudentAttendance(models.Model):
grade = models.CharField(max_length=100, null=True, blank=True)
subject = models.CharField(max_length=100, null=True, blank=True)
admission_number = models.CharField(max_length=100, null=True, blank=True)
first_name = models.CharField(max_length=100, null=True, blank=True)
last_name = models.CharField(max_length=100, null=True, blank=True)
attendance = models.CharField(max_length=100, null=True, blank=True)
held_date = models.DateTimeField(auto_now_add=False, null=True, blank=True)
submit_date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
def __str__(self):
return self.grade
我的看法
@login_required(login_url='login')
def grade8_dashboard(request):
if request.method == "POST":
grade = request.POST.get("grade","")
subject = request.POST.get("subject","")
admission_number = request.POST.get("admission_number","")
first_name = request.POST.get("first_name","")
last_name = request.POST.get("last_name","")
attendance = request.POST.get("attendance","")
# held_date = request.POST.get("held_date","")
submit_date = request.POST.get("submit_date","")
student = StudentAttendance(grade=grade, subject=subject, admission_number=admission_number, first_name=first_name, last_name=last_name, attendance=attendance, submit_date=submit_date)
student.save()
student_details = StudentDetail.objects.all()
context = {
'student_details' : student_details
}
return render(request, 'grade8-dashbaord.html', context)
我的模板/表单
<form method="POST">
{% csrf_token %}
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Admission No</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Present Status</th>
</tr>
</thead>
<tbody>
{% for student in student_details %}
<tr>
<th scope="row">
<div class="form-group ">
<select class="form-control" style="appearance: none;" id="admission_number" name="admission_number">
<option>{{student.admission_number}}</option>
</select>
</div>
</th>
<td>
<div class="form-group">
<select class="form-control" style="appearance: none;" id="first_name" name="first_name">
<option>{{student.first_name}}</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control" style="appearance: none;" id="last_name" name="last_name">
<option>{{student.last_name}}</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<select class="form-control " style="text-align: center;" id="attendance" name="attendance">
<option>Yes</option>
<option>No</option>
</select>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="birthdaytime">Grade :</label>
<select class="form-control" style="appearance: none; font-size: 18px; font-weight: 500;" id="grade" name="grade">
<option>{{user.studying_grade}}</option>
</select>
</div>
<div class="form-group col-md-4">
<label for="birthdaytime">Subject :</label>
<select class="form-control" style="appearance: none; font-size: 18px; font-weight: 500;" id="subject" name="subject">
<option>{{user.assigned_subject}}</option>
</select>
</div>
<div class="form-group col-md-4">
</div>
<button type="submit" class="btn btn-primary btn-lg btn-block">Submit</button>
</div>
</form>
【问题讨论】:
标签: python django django-models django-views django-forms