本文介绍了如何将forloop.counter连接到django模板中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试连接如下:

  {%for choice_dict%} 
{%如果选择=='2'%}
{%与mod| add:forloop.counter | add:.html作为模板%}
{%include template%}
{%endwith%}
{%endif%}
{%endfor%}

但由于某种原因,我只得到mod.html,而不是forloop.counter的数字。有没有人知道发生了什么,我可以做些什么来解决这个问题?谢谢你!

解决方案

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



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

  {%for 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>
...

第二个标签是必需的,因为stringformat标签是自动实现的加入。为了解决这个问题,您可以创建一个自定义过滤器。我使用类似的东西:





将剪切保存为some_app / templatetags / some_name.py



来自django的导入模板

register = template.Library()

def格式(value,arg):

更改默认过滤器stringformat,以不添加%在前面,
所以变量可以放在字符串的任何地方

try :
如果值:
return(unicode(arg))%value
else:
return u''
except(ValueError,TypeError):
返回u''
register.filter('format',format)
  {%load some_name.py%} 

{%for some_list%}
{%with template = forloop.counter | format:mod%s.html%}
< p> ; {{template}}< / p>
{%endwith%}
{%endfor%}


I am already trying to concatenate like this:

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

but for some reason I am only getting "mod.html" and not the forloop.counter number. Does anyone have any idea what is going on and what I can do to fix this issue? Thanks alot!

解决方案

Your problem is that the forloop.counter is an integer and you are using the add template filter which will behave properly if you pass it all strings or all integers, but not a mix.

One way to work around this is:

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

which results in:

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

The second with tag is required because stringformat tag is implemented with an automatically prepended %. To get around this you can create a custom filter. I use something similar to this:

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

save the snipped as 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)

in template:

{% load some_name.py %}

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

这篇关于如何将forloop.counter连接到django模板中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 16:28