我有一个“ EditStudent”页面,显示所有缺席者的姓名。该页面允许管理员将其状态从不存在更改为存在,反之亦然,并在需要时添加备注。页面中显示的信息涉及两个模型。
Namelist model.py:包含所有学生信息,例如studId,studName等。
MarkAtt model.py:保存在课堂上被标记的所有在学学生。因此,此模型不会保存缺席者的姓名。
因此,要检索缺席者的姓名,我做了一些设置,从名称列表到MartAtt模型。
我已经成功保存了在EditStudent页面中所做的所有更改,除了名称之外。
名称列表models.py:
class Namelist(models.Model):
name = models.CharField(max_length=100)
program = models.CharField(max_length=10)
year = models.IntegerField(default=1)
studType = models.CharField(max_length=15)
courseType = models.CharField(max_length=15)
nationality = models.CharField(max_length=20)
VMSAcc = models.CharField(max_length=30)
classGrp = models.ForeignKey('GroupInfo', on_delete=models.SET_NULL,
null=True)
MarkAtt models.py:
class MarkAtt(models.Model):
STATUS=[
('Present', "Present"),
('Absent', "Absent")
]
studName = models.ForeignKey(Namelist,on_delete=models.SET_NULL,blank=True, null=True, default=None)
classGrp = models.ForeignKey(GroupInfo, on_delete=models.SET_NULL, null=True)
currentDate = models.DateField(default=now())
week = models.IntegerField(default=1)
attendance = models.IntegerField(default=100) #100 is present
status = models.CharField(max_length=10, blank=True, null=True, choices=STATUS, default="Present")
remarks = models.CharField(max_length=100, blank=True, null = True, default = " ")
我的Views.py:
def AbsentStudent(request, id=None):
start = None
end = None
w = 1
classGrp = None
attendance = 0
today = datetime.datetime.now()
group = GroupInfo.objects.get(id=id)
q1 = Namelist.objects.filter(classGrp=id).values('name')
q2 = MarkAtt.objects.all().values('studName__name')
q3 = q1.difference(q2)
q4 = Namelist.objects.all()
q5 = q4|q3
form_class = studStatus
form = form_class(request.POST)
if request.method == 'POST':
if form.is_valid():
a = form.save(commit=False)
a.currentDate = datetime.datetime.now().date()
a.classGrp = group
a.attendance = attendance
a.week = w
a.studName = q5
form.save()
return redirect('editStudStatus',id)
else:
form = form = studStatus(request.POST )
context = {
'q4' : q4,
'group' : group,
'form' : form,
'today' :today,
'w' :w
}
return render(request,'studDetails.html',context, {'form':form})
我的模板:
<tbody>
{% for q in q4 %}
<tr>
<td>{{q.name}}</td>
<td>{{q.id}}</td>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<td>{{form.status }}</td>
<td>{{form.remarks}}</td>
<td><button type ="submit" class="btn btn-outline-success" >Submit</button>
</form></td>
</tr>
这是我的表格:
class studStatus(forms.ModelForm):
class Meta:
model = MarkAtt
fields = ['status','remarks']
我从这个实现中收到的错误是:
'NoneType' object has no attribute 'attname'
我试图将学生证作为隐藏文件保存,以便可以将其保存在数据库中。但研究对象未显示在模板中。
如何从未保存在数据库中并与Namelist模型匹配的查询结果中获取studId?