【问题标题】:Remove none fileds from django templates从 django 模板中删除无字段
【发布时间】:2019-12-06 10:33:34
【问题描述】:

我用以下模型创建了一个 django 模板

模型.py

class MaterialRequest(models.Model):
    owner = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='allotment_sales')

    product1 = models.CharField(max_length=500,default=0,blank=True,null=True)
    product1_quantity = models.IntegerField(default=0,blank=True,null=True)

    product2 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product2_quantity = models.IntegerField(default=0,blank=True,null=True)

    product3 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product3_quantity = models.IntegerField(default=0,blank=True,null=True)

    product4 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product4_quantity = models.IntegerField(default=0,blank=True,null=True)

    product5 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product5_quantity = models.IntegerField(default=0,blank=True,null=True)

    product6 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product6_quantity = models.IntegerField(default=0,blank=True,null=True)

    product7 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product7_quantity = models.IntegerField(default=0,blank=True,null=True)

    product8 = models.CharField(max_length=500,default=0, blank=True,null=True)
    product8_quantity = models.IntegerField(default=0,blank=True,null=True)

我尝试使用此视图在模板上显示此数据

def load_salesorder(request):
    so_id = request.GET.get('sales_order')
    s_o = MaterialRequest.objects.filter(pk=so_id)
    print("kits=========",s_o)
    return render(request, 'allotment_so.html', {'sales_order': s_o})

HTML

<table class="table table-bordered">
    <thead>
    <tr>
        <th>Product Short Codes</th>
        <th>Product Quantity</th>
    </tr>
    </thead>

    <tbody>
    <div class="table-container">
        {% for i in sales_order %}
        <tr>
            <td class="align-middle">{{ i.product1 }}</td>
            <td class="align-middle">{{ i.product1_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product2 }}</td>
            <td class="align-middle">{{ i.product2_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product3 }}</td>
            <td class="align-middle">{{ i.product3_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product4 }}</td>
            <td class="align-middle">{{ i.product4_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product5 }}</td>
            <td class="align-middle">{{ i.product5_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product6 }}</td>
            <td class="align-middle">{{ i.product6_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product7 }}</td>
            <td class="align-middle">{{ i.product7_quantity }}</td>
        </tr>
        <tr>
            <td class="align-middle">{{ i.product8 }}</td>
            <td class="align-middle">{{ i.product8_quantity }}</td>
        </tr>

        {% endfor %}

    </tbody>

</table>

但问题在于它还显示了 ORM 的 None 值,而我只想显示包含数据的字段并省略空白值。我该如何解决这个问题,还有更好的方法吗?

【问题讨论】:

  • 您可以在模板上编写{% if %} 语句来检查该上下文是否存在

标签: python django


【解决方案1】:

您可以通过模板中的一些if 语句来实现:

{% for i in sales_order %}

    {% if i.product1 and i.product1_quantity %}
        <tr>
            <td class="align-middle">{{ i.product1 }}</td>
            <td class="align-middle">{{ i.product1_quantity }}</td>
        </tr>
    {% endif %}

    <!-- same for other fields -->

{% endfor %}

【讨论】:

  • 我可以在{% for i in sales_order %}之后添加一个for循环,这样我就不必重复整个代码8次了吗?
  • 我认为通过for 循环是不可能的,但我正在寻找更好的解决方案。
  • 如果你发现了什么,请分享.. 我尝试将 id 添加到td,但它肯定感觉不整洁
  • @RahulSharma 您可以尝试序列化sales_order,然后遍历序列化对象中的字段。检查this
【解决方案2】:

use 可以对每个字段使用 if 语句:

<tr>
    <td class="align-middle">{% if i.product1 %}{{ i.product1 }}{% endif %}</td>
    <td class="align-middle">{% if i.product1_quantity %}{{ i.product1_quantity }}{% endif %}</td>
</tr>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-05
    • 2021-07-18
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2013-12-31
    相关资源
    最近更新 更多