【问题标题】:How can I concatenate forloop.counter to a string in my django template如何将 forloop.counter 连接到我的 django 模板中的字符串
【发布时间】:2011-08-09 04:59:17
【问题描述】:

我已经在尝试像这样连接:

{% for choice in choice_dict %}
    {% if choice =='2' %}
        {% with "mod"|add:forloop.counter|add:".html" as template %}
            {% include template %}
        {% endwith %}                   
    {% endif %}
{% endfor %}    

但由于某种原因,我只得到“mod.html”而不是 forloop.counter 编号。有谁知道发生了什么以及我能做些什么来解决这个问题?非常感谢!

【问题讨论】:

标签: python django django-templates for-loop string-concatenation


【解决方案1】:

尝试不使用使用块“with”

{% for choice in choice_dict %}
    {% if choice =='2' %}
       {% include "mod"|add:forloop.counter|add:".html" %}                   
    {% endif %}
{% endfor %} 

【讨论】:

  • 不错,不幸的是,这不适用于 Django 1.9。
【解决方案2】:

您可能不想在模板中执行此操作,这似乎更像是一项视图工作:(在 for 循环中使用 if)。

chosen_templates=[]
for choice in choice_dict:
  if choice =='2':
    {% with "mod"|add:forloop.counter|add:".html" as template %}
    template_name = "mod%i.html" %index
    chosen_templates.append(template_name)

然后将chosen_templates 传递给您的模板,您将只拥有该模板

{% for template in chosen_templates %}
  {% load template %}
{% endfor %}

另外,我不太明白您为什么要使用字典来选择带有不在字典中的数字的模板。 for key,value in dict.items() 可能就是你要找的。​​p>

【讨论】:

    【解决方案3】:

    您的问题是 forloop.counter 是一个整数,而您正在使用 add 模板过滤器,如果您将所有字符串或所有整数传递给它,它会正常运行,但不是混合。

    解决此问题的一种方法是:

    {% for x in some_list %}
        {% with y=forloop.counter|stringformat:"s" %}
        {% with template="mod"|add:y|add:".html" %}
            <p>{{ template }}</p>
        {% endwith %}
        {% endwith %}
    {% endfor %}
    

    导致:

    <p>mod1.html</p>
    <p>mod2.html</p>
    <p>mod3.html</p>
    <p>mod4.html</p>
    <p>mod5.html</p>
    <p>mod6.html</p>
    ...
    

    第二个 with 标记是必需的,因为 stringformat 标记是使用自动前置的% 实现的。要解决此问题,您可以创建自定义过滤器。我使用类似的东西:

    http://djangosnippets.org/snippets/393/

    将截图保存为 some_app/templatetags/some_name.py

    from django import template
    
    register = template.Library()
    
    def format(value, arg):
        """
        Alters default filter "stringformat" to not add the % at the front,
        so the variable can be placed anywhere in the string.
        """
        try:
            if value:
                return (unicode(arg)) % value
            else:
                return u''
        except (ValueError, TypeError):
            return u''
    register.filter('format', format)
    

    在模板中:

    {% load some_name.py %}
    
    {% for x in some_list %}
        {% with template=forloop.counter|format:"mod%s.html" %}
            <p>{{ template }}</p>
        {% endwith %}
    {% endfor %}
    

    【讨论】:

    • @Ethan:这似乎回答了你的问题,不是吗?
    • @dting - 这很好用,除非你用forloop.counter0 调用它,在这种情况下if value 行将为“0”返回“False”。要解决这个问题,只需将该行更改为 if value is not None
    • 值得注意的是,您需要一个 init.py 在 templatetags 目录中,并重新启动服务器,以便注册,按照:docs.djangoproject.com/en/1.10/howto/custom-template-tags/…
    猜你喜欢
    • 2013-09-02
    • 2011-05-22
    • 2023-03-24
    • 1970-01-01
    • 2010-11-16
    • 2012-03-07
    • 2019-08-03
    相关资源
    最近更新 更多