我在这里有精神障碍。
我的项目有90%可以工作,但是在不使用脏的自定义模板标记的情况下试图找出做某事的最佳方法。
我有2个类(除了默认的django User
模型)。 Echo
,然后Report
。 Report
有2个外键:User
和Echo
。
基本上,用户可以创建一个引用自己和回声的报告。我想要做的是转到主页,它会列出所有的回声(比如~15),然后列出他们报告的回声。
e.g。如果有15个回声,并且用户已经报告了回声3,7和9,那么除了3,7和9之外,将会有一个包含未报告的15个回声的列表,除了3,7和9之外它会说:报告。
大部分都已完成。我使用以下方法在视图中创建了一个回声列表:
echos_list = Echo.objects.order_by('name')
以及登录用户的报告列表:
user_reports = Report.objects.filter(user=request.user)
但是,在模板中,我需要做类似
的操作{% if user_reports.filter(echo=echo.name).count > 0 %}Complete{% else %}Incomplete{% endif %}
...但当然我不能这样做,因为你不能在模板中使用过滤器。你会建议我做什么?
查看:
from django.shortcuts import render_to_response, get_object_or_404, redirect
from dseapp.models import Echo,Report
from django.template import RequestContext
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
# Create your views here.
@login_required
def dse_index(request):
echos_list = Echo.objects.order_by('name') #We'll use this to create the entire list of echos
user_reports = Report.objects.filter(user=request.user) #We'll use this to make a list of reports for the current user
return render_to_response('dseapp/dse.html', {'echos_list':echos_list,'user_reports':user_reports},context_instance=RequestContext(request))
模板:
<table>
<tr>
<th>Echo</th>
<th>Status</th>
</tr>
{% for echo in echos_list %}
<tr>
<td>{{echo.name}}</td>
<td><a href="/report/{{echo.name}}">
{% if user_reports.filter(echo=echo.name).count > 0 %}Complete{% else %}Incomplete{% endif %}
</a></td>
</tr>
{% endfor %}
</table>
<a href="/logout/">Logout</a>
型号:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Echo(models.Model):
name = models.CharField(max_length=200,blank=True)
image1_desc = models.CharField(max_length=200,blank=True)
image1_url = models.CharField(max_length=200,blank=True)
image2_desc = models.CharField(max_length=200,blank=True)
image2_url = models.CharField(max_length=200,blank=True)
image3_desc = models.CharField(max_length=200,blank=True)
image3_url = models.CharField(max_length=200,blank=True)
image4_desc = models.CharField(max_length=200,blank=True)
image4_url = models.CharField(max_length=200,blank=True)
hr_rest = models.IntegerField(blank=True,null=True)
hr_low = models.IntegerField(blank=True,null=True)
hr_high = models.IntegerField(blank=True,null=True)
lesion1_ffr = models.DecimalField(max_digits=4,decimal_places=2,blank=True,null=True)
lesion1_ifr = models.DecimalField(max_digits=4,decimal_places=2,blank=True,null=True)
lesion2_ffr = models.DecimalField(max_digits=4,decimal_places=2,blank=True,null=True)
lesion2_ifr = models.DecimalField(max_digits=4,decimal_places=2,blank=True,null=True)
lesion3_ffr = models.DecimalField(max_digits=4,decimal_places=2,blank=True,null=True)
lesion3_ifr = models.DecimalField(max_digits=4,decimal_places=2,blank=True,null=True)
lesion4_ffr = models.DecimalField(max_digits=4,decimal_places=2,blank=True,null=True)
lesion4_ifr = models.DecimalField(max_digits=4,decimal_places=2,blank=True,null=True)
lesion5_ffr = models.DecimalField(max_digits=4,decimal_places=2,blank=True,null=True)
lesion5_ifr = models.DecimalField(max_digits=4,decimal_places=2,blank=True,null=True)
def __unicode__(self):
return self.name
class Report(models.Model):
user = models.ForeignKey(User)
echo = models.ForeignKey(Echo)
answer_1 = models.IntegerField()
answer_2 = models.IntegerField()
answer_3 = models.IntegerField()
answer_4 = models.IntegerField()
answer_5 = models.IntegerField()
answer_6 = models.IntegerField()
answer_7 = models.IntegerField()
answer_8 = models.IntegerField()
answer_9 = models.IntegerField()
answer_10 = models.IntegerField()
answer_11 = models.IntegerField()
answer_12 = models.IntegerField()
answer_13 = models.IntegerField()
answer_14 = models.IntegerField()
answer_15 = models.IntegerField()
answer_16 = models.IntegerField()
answer_17 = models.IntegerField()
ischaemic = models.BooleanField()
confidence = models.IntegerField()
def __unicode__(self):
return self.user.__unicode__() + " / " + self.echo.__unicode__()
答案 0 :(得分:1)
您需要在视图中计算此值。 echos_list
应该是一个包含字典的列表,对于每个Echo
,它是否已完成。
echos_list = []
for echo in Echo.objects.order_by('name'):
echos_list.append({
'object': echo,
'complete': user_reports.filter(echo=echo.name).count() > 0
}]
然后,您可以将模板更新为:
{% for echo in echos_list %}
<tr>
<td>{{echo.object.name}}</td>
<td><a href="/report/{{echo.object.name}}">
{% if echo.complete %}Complete{% else %}Incomplete{% endif %}
</a></td>
</tr>
{% endfor %}