如何在 Django 模板中创建计数器循环? [英] how to create counter loop in django template?

查看:42
本文介绍了如何在 Django 模板中创建计数器循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何在django中编写下面的c代码

for(c=0; c<5; c++)//做一点事

我试过下面的代码,但它给了我一个错误

{% for(c=0; c<5; c++)%}<div class="tab-content"><h1 class="tab" title="第 1 页的标题">第 1 页</h1><p>这是容器1上标签1的内容</p>

{% 结束为 %}

解决方案

渲染模板时,可能会传递范围

render_to_response('template_x.html', {'range5': range(5)})

在html模板中,大概是这样的

{% for i in range5 %}<div class="tab-content"><h1 class="tab" title="title for page {{i}}">Page {{i}}</h1><p>这是容器{{i}}上标签{{i}}的内容</p>

{% 结束为 %}

can any tell me how can I write below code of c in django

for(c=0; c<5; c++)
  //do something

i had tried below code but it gives me an error

{% for(c=0; c<5; c++)%}
     <div class="tab-content">
    <h1 class="tab" title="title for page 1">Page 1</h1>
    <p>This is the content of tab 1 on container 1</p>
     </div>     
{% endfor %}

解决方案

When you render your template, you may pass range

render_to_response('template_x.html', {'range5': range(5)})

And in html template, probably like this

{% for i in range5 %}
    <div class="tab-content">
    <h1 class="tab" title="title for page {{i}}">Page {{i}}</h1>
    <p>This is the content of tab {{i}} on container {{i}}</p>
    </div>  
{% endfor %}

这篇关于如何在 Django 模板中创建计数器循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆