如何在html中显示视图中的动态django数据?
这似乎是个简单的问题,但我就是做不好 我有三个django模型:ProjectName、ProjectBudget和ProjectActualCost 项目名称存储项目名称(即项目X)。如何在html中显示视图中的动态django数据?,django,django-views,Django,Django Views,这似乎是个简单的问题,但我就是做不好 我有三个django模型:ProjectName、ProjectBudget和ProjectActualCost 项目名称存储项目名称(即项目X)。 ProjectBudget存储每个预算的项目名称(作为外键(FK))、预算名称以及总预算和日期。ie(项目X,酒店,600),(项目X,租赁,500)。'“600”指600美元。 ProjectActualCost存储发生的各项成本(分项成本)(即酒店:100,租金:50,食品:100)及其日期。因此,它存储“
ProjectBudget存储每个预算的项目名称(作为外键(FK))、预算名称以及总预算和日期。ie(项目X,酒店,600),(项目X,租赁,500)。'“600”指600美元。 ProjectActualCost存储发生的各项成本(分项成本)(即酒店:100,租金:50,食品:100)及其日期。因此,它存储“项目名称”(作为FK)、“预算名称”(作为FK)、实际使用的项目名称和日期。 ie(项目X,酒店,100,10/03/2019),(项目X,酒店,100,10/06/2019), (项目X,租金,50,04/10/2019) 我试图在html表格中呈现“项目名称”、“预算”和“实际成本”“项目名称”和“预算”显示正确,但“实际成本”仅显示所有成本项目的一个金额 型号:
class ProjectName(models.Model):
project_name = models.CharField('Name',max_length = 15,blank = False)
def __str__(self):
return self.project_name
class ProjectBudget(models.Model):
project_name = models.ForeignKey(ProjectName,on_delete = models.CASCADE,
null = True)
budget_name = models.CharField('Budget Name'max_length = 50
total_budget = models.DecimalField('Total Budget',max_digits = 9,decimal_places=2)
def __str__(self):
return self.budget_name
class ProjectActualCost(models.Model):
project_name = models.ForeignKey(ProjectName,on_delete = models.CASCADE, null = True)
cost_description = models.ForeignKey(ProjectBudget,on_delete = models.CASCADE,null=True)
actual_used = models.DecimalField('Actual Used',max_digits = 15,decimal_places = 2)
观点:
def budgetview(request,project_id):
budget_items = ProjectBudget.objects.filter(project_name_id =project_id)
for budget in budget_items:
budget_id = budget.id
actual_cost =
ProjectActualCost.objects.filter(cost_description_id=
budget_id).aggregate(Sum('actual_used')).get('actual_used__sum') or 0
print(budget.cost_description,":",actual_cost)#To test the output on StatReloader
budget_template = "budget_view.html"
context = {"budget_items":budget_items ,"actual_cost":actual_cost}
return render(request,budget_template,context)
budget_view.html:
<table>
<thead>
<tr>
<th>Project Name</th>
<th>Budget Name</th>
<th>Total Budget</th>
<th>Total Used</th>
</tr>
<tbody>
{% for item in budget_items%}
<tr>
<td>{{item.project_name}}</td>
<td>{{item.cost_description}}</td>
<td>{{item.total_budget}}</td>
<td>{{actual_cost}}</td>
</tr>
{%endfor%}
</tbody>
</table>
StratReloader显示:
Hotel:200
Rental:50
但是当我呈现budget_view.html时,我得到:
Project Name|Budget Name | Total Budget| Total Used
Project X | Hotel | 600 | 50
Project X | Rental | 500 | 50
我认为问题在于,当for循环在html中运行时,budget_id不是动态的 您只有一个
实际成本
变量,您可以在整个循环中重复覆盖该变量
但您不想使用循环,也不想使用聚合。您希望使用注释
,它一次向数据库请求查询集中每个项的聚合值
budget_items = ProjectBudget.objects.filter(project_name_id=project_id).annotate(
actual_cost=Sum('projectactualcost__actual_used')
)
budget_template = "budget_view.html"
context = {"budget_items": budget_items}
return render(request, budget_template, context)
在模板中:
{% for item in budget_items %}
<tr>
<td>{{ item.project_name }}</td>
<td>{{ item.cost_description }}</td>
<td>{{ item.total_budget }}</td>
<td>{{ item.actual_cost }}</td>
</tr>
{% endfor %}
{预算中项目的%u项目%}
{{item.project_name}
{{item.cost_description}
{{item.total_budget}
{{item.actual_cost}
{%endfor%}
正是我想要的。谢谢!如果答案对你有帮助,别忘了接受它,作为未来搜索者的标志。谢谢丹尼尔。我确实接受了答案,但我的声誉很低。声誉不相关,您需要单击复选标记
{% for item in budget_items %}
<tr>
<td>{{ item.project_name }}</td>
<td>{{ item.cost_description }}</td>
<td>{{ item.total_budget }}</td>
<td>{{ item.actual_cost }}</td>
</tr>
{% endfor %}