gpt4 book ai didi

python - 数据在我的 django 模板中无法正确显示

转载 作者:太空宇宙 更新时间:2023-11-03 16:14:28 25 4
gpt4 key购买 nike

我需要帮助弄清楚如何以一种给我带来问题的方式设置我的 html 表,如果有人可以帮助我弄清楚如何修复它并使其看起来像第二张图片会很棒。

*旁注我正在使用 Django

首先,我将在此 View /模板中使用三个模型。它们被称为Sheet、Dimension、Inspeciton_vals,我的Dimension模型有一个名为sheet_id的伪造 key ,它链接到sheet,我的Inspeciton_vals模型有一个链接到Dimension的外键。

这是我的观点.py

@login_required 
def shipping(request, id):
sheet_data = Sheet.objects.get(pk=id)
work_order = sheet_data.work_order
customer_data = Customer.objects.get(id=sheet_data.customer_id)
customer_name = customer_data.customer_name
title_head = 'Shipping-%s' % sheet_data.work_order
complete_data = Sheet.objects.raw("""select s.id, s.work_order, d.target, i.reading, d.description, i.serial_number from app_sheet s left join app_dimension d on s.id = d.sheet_id
left join app_inspection_vals i on d.id = i.dimension_id""")


for c_d in complete_data:
dim_description = Dimension.objects.filter(sheet_id=c_d.id).values_list('description', flat=True).distinct()
dim_id = Dimension.objects.filter(sheet_id=c_d.id)[:1]
for d_i in dim_id:
dim_data = Inspection_vals.objects.filter(dimension_id=d_i.id)
sample_size = dim_data


return render(request, 'app/shipping.html',
{
'work_order': work_order,
'sample_size': sample_size,
'customer_name': customer_name,
'title': title_head,
'complete_data': complete_data,
'dim_description': dim_description,
})

这是我的模型

class Sheet(models.Model): 
objects = SheetManager()

create_date = models.DateField()
updated_date = models.DateField()
customer_name = models.CharField(max_length=255)
part_number = models.CharField(max_length=255)
part_revision = models.CharField(max_length=255)
work_order = models.CharField(max_length=255)
purchase_order = models.CharField(max_length=255)
sample_size = models.IntegerField()
sample_scheme = models.CharField(max_length=255)
overide_scheme = models.IntegerField()
template = models.IntegerField()
sample_schem_percent = models.IntegerField()
critical_dimensions = models.IntegerField()
closed = models.IntegerField()
serial_index = models.CharField(max_length=255)
drawing_number = models.CharField(max_length=255)
drawing_revision = models.CharField(max_length=255)
heat_number = models.CharField(max_length=255)
note = models.CharField(max_length=255)
valc = models.CharField(max_length=255)

class Dimension(models.Model):
description = models.CharField(max_length=255)
style = models.CharField(max_length=255)
created_at = models.DateField()
updated_at = models.DateField()
target = models.IntegerField()
upper_limit = models.IntegerField()
lower_limit = models.IntegerField()
inspection_tool = models.CharField(max_length=255)
critical = models.IntegerField()
units = models.CharField(max_length=255)
metric = models.CharField(max_length=255)
target_strings = models.CharField(max_length=255)
ref_dim_id = models.IntegerField()
nested_number = models.IntegerField()

met_upper = models.IntegerField()
met_lower = models.IntegerField()
valc = models.CharField(max_length=255)
sheet = models.ForeignKey(Sheet, on_delete=models.CASCADE, default=DEFAULT_FOREIGN_KEY)

class Inspection_vals(models.Model):
created_at = models.DateField()
updated_at = models.DateField()
reading = models.IntegerField(null=True)
reading2 = models.IntegerField(null=True)
reading3 = models.IntegerField(null=True)
reading4 = models.IntegerField(null=True)
state = models.CharField(max_length=255)
state2 = models.CharField(max_length=255)
state3 = models.CharField(max_length=255)
state4 = models.CharField(max_length=255)
approved_by = models.CharField(max_length=255)
approved_at = models.DateField(null=True, blank=True)
dimension = models.ForeignKey(Dimension, on_delete=models.CASCADE, default=DEFAULT_FOREIGN_KEY)
serial_number = models.IntegerField(default=1)

最后这是我的模板我想做的是让我的标题成为序列号。这将基于我的图纸模型上的sample_size,所以假设我有 24 个样本大小,显示 20 个水平行。接下来,我现在将在右侧显示尺寸描述,sample_size 为 24,我将有 2 个尺寸链接到我的图纸模型,这也会每次发生变化。最后,我想将每个 Inspection_val 和维度的读数放入表的其余部分 - 因此,如果我有 2 个维度,样本大小为 24,我应该有 48 个 Inspectiton_vals,我想对相应的维度和序列使用正确的读数数字。这是我到目前为止所拥有的--

<div class="container"> 
<div class="row">
<div>
<table >
<thead>
<tr>
<th>Serial Number</th>
{% for ss in sample_size %}
<th>{{ ss.serial_number }}</th>
{% endfor %}
</tr>
<tr>
{% for r_c in complete_data %}
<th> {{ r_c.reading }} </th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for desc in dim_description.all %}
<tr>
<th> {{ desc }}</th>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>

这是现在的样子 Here is what It looks like now

这就是我想要的样子

Here is what I would like it to look like

这里的奖励是我的数据的样子

My Data

修复回答后建议仍然没有按照我希望的方式显示..

<div class="container"> 
<div class="row">
<div>
<table >
<thead>
<tr>
<th>Serial Number</th>
{% for ss in sample_size %}
<th>{{ ss.serial_number }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for desc in dim_description.all %}
<tr>
<td> {{ desc }}</td>
</tr>
{% for r_c in complete_data %}
<td> {{ r_c.reading }} </td>
{% endfor %}

</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>

现在的样子

New pic

根据@Michael Platt 建议更新了代码

<div class="container"> 
<div class="row">
<div>
<table >
<thead>
<tr>
<th>Serial Number</th>
{% for ss in sample_size %}
<th>{{ ss.serial_number }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for desc in dim_description.all %}
<tr>
<td> {{ desc }}</td>
{% for r_c in complete_data %}
<td> {{ r_c.reading }} </td>
{% endfor %}
{% endfor %}
</tr>
</tbody>
</table>
</div>
</div>
</div>

@Michael Platt 帮助解决了 html 问题,现在我希望能够将读数分成两半,这样 24 将位于内部 od 行,接下来的 24 将位于外部 od 行。

Answer pic

最佳答案

好吧,我知道这是你的问题:

<tbody>
{% for desc in dim_description.all %}
<tr>
<td> {{ desc }}</td>
{% for r_c in complete_data %}
<td> {{ r_c.reading }} </td>
{% endfor %}
{% endfor %}
</tr>
</tbody>

您有一个额外的<\tr>就在您的第二个{% endfor %}之前在 <tbody>标签。我已经在上面更改了它,所以我认为它会提供您想要的正确设计。如果它没有让我知道,但在我这边测试有点困难,因为我没有启动并运行该应用程序:-)

干杯,

关于python - 数据在我的 django 模板中无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39038761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com