Python-Django:ifchanged模板标签 [英] Python-Django: ifchanged template tag

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

问题描述

在这里我正在从employee.Same的员工获得2或3个记录的持续时间。
所以如果雇员的持续时间和添加和显示,如果员工ID更改然后
再次计算员工持续时间并显示
我需要fo找到每个员工ID有多少条记录,通过 {{forloop.counter}} 个别



这里我的问题是...


  1. 如果我的循环来到 {%ifchanged eachSc.laEmpNum%} 然后 {{ result.0}} 。如果再次 {{result.1}} {{result.2}} 那么...


  2. 如果我的循环来到 {%ifchanged eachSc.laEmpNum%} 然后我的 {{forloop.counter}} 应该以0(零)开始。


result = [5.5,4.5,1.3] 我从 view.py

  {DBShots1%中为eachSc的%} 

< tr>
{%ifchanged eachSc.laEmpNum%}

< td bgcolor =#FFFACDwidth =1%> Tot = {{result}}< / td>
{%endifchanged%}

< / tr>
< td bgcolor =#FFFACDwidth =1%> {{forloop.counter}}< / td>

< td bgcolor =#CCFACDwidth =1%> {{eachSc.sName}}< / td>

< td bgcolor =#CCF0F5width =1%> {{eachSc.duration}}< / td>

< td bgcolor =#CCFACDwidth =1%> {{eachSc.frames}}< / td>

< td bgcolor =#CCFACDwidth =5%> {{GetEmpDept}} - {{getEmpName.emp_name}} - {{eachSc.laEmpNum}}< / td> ;
{%endfor%}


解决方案

我相信这里有几个问题要处理。首先我看到你想打印一个result变量的项目,具体取决于forloop计数器的位置。这在django模板中是不可能的(出于各种公平的原因)。要在视图中快速解决此问题,您可以定义一个自定义过滤器,只需返回指定索引上的列表项。你可以把它放在你的模板标签/ myfilters.py中:

  from django import template 
register = template.Library )

@ register.filter
def getitem(mylist,index):
return mylist [index]

然后,当'laEmpNum'更改时,重新设置forloop计数器,您应该使用'regroup'django模板标签,如下所示:



$ {code} {%load myfilters%}
{%将每个ScList%的LaEmpNum重新组合DBShots1
{each eachScList中的每个ScGr%%}
{% for eachSc在每个ScGrp.list%}
< tr>
< td bgcolor =#FFFACDwidth =1%> {{forloop.counter}}< / td>
< td bgcolor =#CCFACDwidth =1%> {{eachSc.sName}}< / td>
< td bgcolor =#CCF0F5width =1%> {{eachSc.duration}}< / td>
< td bgcolor =#CCFACDwidth =1%> {{eachSc.frames}}< / td>
< td bgcolor =#CCFACDwidth =5%> {{GetEmpDept}} - {{getEmpName.emp_name}} - {{eachSc.laEmpNum}}< / td>
< / tr>
{%endfor%}
< tr>
< td> < / TD>< TD> < / TD>
< td bgcolor =#FFFACDwidth =1%> Tot = {{result | getitem:forloop.counter0}}< / td>
< / tr>
{%endfor%}

第一行加载我们的自定义过滤器库。最后一部分使用自定义过滤器根据laEmpNum组合器上的迭代来检索结果项。


Here i am getting employee with duration from database.Same employee with 2 or 3 records. so gettting employee duration and adding and displaying,if employee ID changed then again it calculate the employee duration and displaying I need fo find each employee ID has how many records,through {{ forloop.counter }}individually

Here my problems are...

  1. If my loop coming to {% ifchanged eachSc.laEmpNum %} then {{ result.0 }}. If again then {{ result.1 }} and {{ result.2 }} then so on...

  2. If my loop coming to {% ifchanged eachSc.laEmpNum %} then my {{ forloop.counter }} should start with 0(zero) again.

result = [5.5, 4.5, 1.3] which i am getting from view.py

{% for eachSc in DBShots1 %}

<tr>
    {% ifchanged eachSc.laEmpNum %} 

    <td bgcolor="#FFFACD" width="1%">Tot={{ result }}</td>
    {% endifchanged %}

</tr>
    <td bgcolor="#FFFACD" width="1%">{{ forloop.counter }} </td> 

    <td bgcolor="#CCFACD" width="1%">{{ eachSc.sName }}</td>

    <td bgcolor="#CCF0F5" width="1%">{{ eachSc.duration }}</td>

    <td bgcolor="#CCFACD" width="1%">{{ eachSc.frames }}</td>

    <td bgcolor="#CCFACD" width="5%">{{ GetEmpDept }} - {{ getEmpName.emp_name }} - {{ eachSc.laEmpNum }}</td>
{% endfor %}

解决方案

I believe there are a couple of issues to deal with here. First of all I see you want to print an item of the 'result' variable, depending on the position of the forloop counter. This is not directly possible in django templates (for various fair reasons). To quickly solve this without reorganizing your data in the view, you can define a custom filter that simply returns a list item on the specified index. You could put this in your templatetags/myfilters.py:

from django import template
register = template.Library()

@register.filter
def getitem(mylist, index):
    return mylist[index]

Then, to reset the forloop counter when 'laEmpNum' changes you should use the 'regroup' django template tag like this:

{% load myfilters %}
{% regroup DBShots1 by laEmpNum as eachScList %}
{% for eachScGrp in eachScList %}
    {% for eachSc in eachScGrp.list %}
    <tr>
      <td bgcolor="#FFFACD" width="1%">{{ forloop.counter }} </td> 
      <td bgcolor="#CCFACD" width="1%">{{ eachSc.sName }}</td>
      <td bgcolor="#CCF0F5" width="1%">{{ eachSc.duration }}</td>
      <td bgcolor="#CCFACD" width="1%">{{ eachSc.frames }}</td>
      <td bgcolor="#CCFACD" width="5%">{{ GetEmpDept }} - {{ getEmpName.emp_name }} - {{ eachSc.laEmpNum }}</td>
     </tr>
    {% endfor %}
    <tr>
        <td> </td><td> </td>
        <td bgcolor="#FFFACD" width="1%">Tot={{ result|getitem:forloop.counter0 }}</td>
    </tr>
{% endfor %}

The first line loads our custom filter library. The last part uses the custom filter to retrieve a result item based on the iteration over the 'laEmpNum' groupper.

这篇关于Python-Django:ifchanged模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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