Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is that the forloop.counter is an integer and you are using the <code>add</code> template filter which will behave properly if you pass it all strings or all integers, but not a mix.</p> <p>One way to work around this is:</p> <pre><code>{% for x in some_list %} {% with y=forloop.counter|stringformat:"s" %} {% with template="mod"|add:y|add:".html" %} &lt;p&gt;{{ template }}&lt;/p&gt; {% endwith %} {% endwith %} {% endfor %} </code></pre> <p>which results in:</p> <pre><code>&lt;p&gt;mod1.html&lt;/p&gt; &lt;p&gt;mod2.html&lt;/p&gt; &lt;p&gt;mod3.html&lt;/p&gt; &lt;p&gt;mod4.html&lt;/p&gt; &lt;p&gt;mod5.html&lt;/p&gt; &lt;p&gt;mod6.html&lt;/p&gt; ... </code></pre> <p>The second with tag is required because stringformat tag is implemented with an automatically prepended <code>%</code>. To get around this you can create a custom filter. I use something similar to this:</p> <p><a href="http://djangosnippets.org/snippets/393/" rel="noreferrer">http://djangosnippets.org/snippets/393/</a></p> <p>save the snipped as some_app/templatetags/some_name.py</p> <pre><code>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) </code></pre> <p>in template:</p> <pre><code>{% load some_name.py %} {% for x in some_list %} {% with template=forloop.counter|format:"mod%s.html" %} &lt;p&gt;{{ template }}&lt;/p&gt; {% endwith %} {% endfor %} </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload