【问题标题】:Summarizing inside a Django template在 Django 模板中进行总结
【发布时间】:2010-09-20 02:30:42
【问题描述】:

我在 django 中有以下模板,我想获取每个文档对象的最后 2 列的总数

{% for documento in documentos %}
    {% for cuenta in documento.cuentasxdocumento_set.all %}
        <tr {% cycle 'class="gray"' '' %} >
            {% if forloop.first %}
                    <td>{{ documento.fecha_creacion.date }}</td>
                    <td>{{ cuenta.cuenta.nombre }}</td>
                    <td>
                        {% if cuenta.monto >= 0 %}
                            {{ cuenta.monto}}
                        {% endif %}
                    </td>
                    <td>
                        {% if cuenta.monto <= 0 %}
                            {{ cuenta.monto }}
                        {% endif %}
                    </td>
            {% else %}

                    <td colspan="4"></td>
                    <td>{{ cuenta.cuenta.codigo }}</td>
                    <td>{{ cuenta.cuenta.nombre }}</td>
                    <td>
                        {% if cuenta.monto <= 0 %}
                            {{ cuenta.monto }}
                        {% endif %}
                    </td>
                    <td>
                        {% if cuenta.monto >= 0 %}
                            {{ cuenta.monto }}
                        {% endif %}
                    </td>

            {% endif %}
        </tr>
    {% endfor %}
    <tr>
        <td colspan="1"></td>
        <td>Document Total</td>
        <td></td>
        <td></td>
    </tr>
{% endfor %}

这一切都是使用以下模型完成的,为了这个问题的目的,这些模型被简化了

class Documento(models.Model):
    numero_impreso = models.CharField(max_length=50)
    fecha_creacion = models.DateTimeField(auto_now_add = True)


    cuentas = models.ManyToManyField('CuentaContable', through = 'CuentasXDocumento', null = True)

    def __unicode__(self):
        return self.tipo.nombre + ": " + self.numero_impreso

class CuentasXDocumento(models.Model):
    cuenta = models.ForeignKey('CuentaContable')
    documento = models.ForeignKey('Documento')

    monto = models.DecimalField(max_digits= 14, decimal_places = 6)
    linea = models.IntegerField()

class CuentaContable(models.Model):
    codigo = models.CharField(max_length=50)
    nombre = models.CharField(max_length=100)    
    def __unicode__(self):
        return self.nombre

最后我很抱歉英语不好:)

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:

    根据我使用 Django 的经验,我想说这些事情在模板中并不容易完成。我尝试在视图而不是模板中进行计算。

    我的建议是在视图而不是模板中计算您需要的两个总和。

    众生说,使用custom filters and tags 可以在模板中做一些工作。使用过滤器可能看起来像这样:

    <td>{% documento.cuentasxdocumento_set.all | sum_monto:"pos" %}</td>
    <td>{% documento.cuentasxdocumento_set.all | sum_monto:"neg" %}</td>
    

    过滤器有两个参数,一个是您传递给过滤器的值,另一个是您可以用来控制其行为的参数。您可以使用最后一个参数告诉sum_monto 对正值或负值求和。

    这是一个未经测试的快速过滤器实现:

    from django import template
    
    register = template.Library()
    
    @register.filter
    def sum_monto(cuentas, op):
        if op == "pos":
             return sum(c.monto for c in cuentas if c.monto > 0)
        else
             return sum(c.monto for c in cuentas if c.monto < 0)
    

    【讨论】:

    • 谢谢,我不会想到过滤器,我需要在模板中进行,因为它是任意数量的 Documentos
    • 任意数量的文档如何阻止您计算视图中的总和?
    • 我不知道...也许是因为我是 django 的菜鸟,更习惯 PHP 的方式,
    • 我的意思是,在 php 中我会在循环内有一个临时变量,在这里我会进行计算并最终回显....但是在视图中?这里?我只是不知道该怎么做
    • 好的。也许您对 Django 术语视图有一些困惑?在 Django 中,视图是处理您的请求并调用模板的一段代码。在您的情况下,您可能正在执行一些数据库查询来获取您的文档。在同一个地方,您可以计算您的总和并将它们发送到模板,就像您发送文件一样。
    猜你喜欢
    • 2021-08-09
    • 2014-02-19
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    相关资源
    最近更新 更多