开发者

Django, simplest forloop, how? (i=0; i<20; i++)

I just need to generate some test content for my template. Something like:

{{ for i < 20 }}
<img src="image-{{i}}.jpg " />
{{ endfor }}

I 开发者_开发百科have no list, how should I proceed?


Well, it stucks up if you don't have a list or a iterable on template. DJango doesn't provide a default way to write dummy data. But it does provide lorem tag to get some dummy text. See here.

But you can do an exercise though to get working what exactly you are trying to do. Create a custom templatetag, as myutilities.py and add to templatetags directory of your app.

from django import template        
register = template.Library()  

@register.filter  
def range(value):  
    return range(value)

Template

{% load myutilities %}  
{% for i in 50|range %}  
   {{ forloop.counter }}<img src="image-{{i}}.jpg " />
{% endfor %}  


http://djangosnippets.org/snippets/779/

http://djangosnippets.org/snippets/1357/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜