模板:
{% for level in levels %}
{% if level.todo_set.all in tasks %}
<li>
<a href="javascript:;"><img src="{{
level.badge.url }}" alt="" /></a>
</li>
{% else %}
<li>
<a href="javascript:;"><img src="{{
level.locked_badge.url }}" alt="" /></a>
</li>
{% endif %}
{% endfor %}
views.py:
@login_required(login_url='/account/login/')
def StudentPublicProfile(request,pk=None):
student = User.objects.get(pk=pk)
levels = Level.objects.all()
todos=ToDo.objects.all()
list=[]
tasks=Task.objects.filter(student=student)
for task in tasks:
list.append(task.todo)
context={
'student': student,
'levels' : levels,
'tasks' : list,
'todos' : todos,
}
return render(request, 'student_public_profile2.html',
context)
models.py:
class Level(models.Model):
number = models.IntegerField()
badge = models.ImageField()
locked_badge = models.ImageField()
timestamp =
models.DateTimeField(auto_now_add=True,auto_now=False,blank=True,
null=True)
unlock = models.CharField(max_length=10,default="A")
def __str__(self):
return str(self.number)
def get_absolute_url(self):
return reverse('student:level-detail', kwargs={'pk': self.pk})
class ToDo(models.Model):
level = models.ForeignKey(Level, on_delete=models.CASCADE)
name = models.CharField(max_length=150)
description = models.TextField()
timestamp =
models.DateTimeField(auto_now_add=True,auto_now=False,blank=True,
null=True)
def __str__(self):
return self.name
class Task(models.Model):
level = models.ForeignKey(Level, on_delete=models.CASCADE)
todo = models.ForeignKey(ToDo, on_delete=models.CASCADE)
student = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=150)
content = models.TextField()
timestamp = models.TimeField(auto_now=True)
datestamp = models.DateField( auto_now=True)
like =
models.ManyToManyField(User,related_name='user_likes',blank=True)
is_verified=models.BooleanField(default=False,blank=True)
def __str__(self):
return self.title
基本上,每个级别都包含多个待办事项。我想检查是否所有级别的待办事项都包含在任务列表中。这就是我在模板中所做的。但是我没有得到正确的结果。这里可能是什么问题?在这里,我要检查某个级别的所有待办事项是否由特定用户完成。只有待办事项完成后,它才会保存在任务中
答案 0 :(得分:1)
您最好在view
内部处理此问题。
for level in levels_list:
if Todo.objects.filter(level=level).count() == Task.objects.filter(student=student, level=level).count():
return (level completion code)
答案 1 :(得分:0)
levellist=[]
for level in levels:
if ToDo.objects.filter(level=level).count()==
Task.objects.filter(student=student,level=level).count():
levellist.append(level.number)
perc=(len(levellist)/len(levels))*100
在模板中:
{% for level in levels %}
{% if level.number in levellist %}
<li>
<a href="javascript:;"><img src="{{ level.badge.url }}"
alt=""/></a>
</li>
{% else %}
<li>
<a href="javascript:;"><img src="{{ level.locked_badge.url }}"
alt=""/></a>
</li>
{% endif %}
{% endfor %}
找到了解决方法!