总结一下Django模板 [英] Summarizing inside a Django template

查看:104
本文介绍了总结一下Django模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following template in django, i want to get the totals of the last 2 columns for each of my document objects

{% 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 %}

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

This is all done using the following models, which are simplified for the purpose of this question

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

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

Finally I'm sorry for the bad english :)

推荐答案

根据Django的经验,我会说这些东西在模板中不容易做到。我试图在视图中做我的计算,而不是模板。

From my experience with Django, I would say that these things aren't easily done in the template. I try to do my calculations in the view instead of the template.

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

生物说,可以使用自定义过滤器和标签。使用过滤器可能如下所示:

That beings said, it is possible to do some work in the template using custom filters and tags. Using filters it might look like this:

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

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

Filters take two arguments, the value that you pass to the filter and an argument that you can use to control its behavior. You could use the last argument to tell sum_monto to sum the positive values or the negative values.

这是一个快速未经测试的过滤器实现从我的头顶部:

This is a quick untested filter implementation off the top of my head:

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)

这篇关于总结一下Django模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆