串联的Django模板标签正在打印串联的字符串而不是值

时间:2019-05-03 20:24:01

标签: django django-templates django-template-filters

我正在尝试将django模板分组器与字符串连接起来,以便将整个内容用作模板标签。

这是我的模板:

    {% block content %}
    REPORT:
    </br>
    {% regroup context by dealid as deal_list %}
        {% for dealid in deal_list %}
        {{dealid.grouper}}
            {% regroup dealid.list by inventory_status as stone_list%}
            {% for inventory_status in stone_list %}
                {{inventory_status.grouper}}
                <table>
                    <thead>
                        <tr>
                        <th>StoneID</th>
                        <th>Ct</th>
                        <th>Cost</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for stone in inventory_status.list %}
                        <tr>
                        <td>{{ stone.stoneid }}</td>
                        <td>{{ stone.ct_in|floatformat:2 }}</td> 
                        <td>{{ stone.cost_purchase|prepend_dollars }}</td>
                        </tr>
                        {% endfor %}
                    {% endfor %}
                </tbody>
                </table>
PROBLEM --->    <b>Subtotal: {{dealid_id.grouper WITH VARIOUS CONCATENATIONS}} <--</b> 
               </br>
            </br>
        {% endfor %}
    {% endblock content %}

我尝试过:

Subtotal: {% with dealid_id.grouper|subtotalT as t %} {{ t }} {% endwith %} - get the string printed 

Subtotal: {{"totals."|add:dealid_id.grouper|stringformat:"a/s/r"|add:".Sold.sum_by_deal"}} - get just the last part of the string

但是我只能打印出字符串:

Subtotal: totals.33.Sold.sum_by_deal, for example
which, by the way, works when hardcoded:
Subtotal: {{totals.33.Sold.sum_by_deal}}

我还尝试创建自定义过滤器:

@register.filter
def subtotalT(s1):
    return 'totals.'+str(s1)+'.Sold.sum_by_deal'
    # https://stackoverflow.com/questions/37933259/how-to-use-django-template-tag-within-another-tag

并尝试像这样使用它:

Subtotal: {{dealid_id.grouper|subtotalT}} - get the string printed

但是,它再次打印字符串而不是值。

我想念什么吗?这不可行吗?我将非常感谢任何人的见解...谢谢!

0 个答案:

没有答案