如何在django模板中的4个元素之后创建div
我希望你很好 我想在我的页脚上显示我的城市 我想在每个div中添加4个城市,然后在div中添加4个城市。我想创建一个新div并添加4个城市,以此类推 以下是html参考文件:如何在django模板中的4个元素之后创建div,django,django-templates,Django,Django Templates,我希望你很好 我想在我的页脚上显示我的城市 我想在每个div中添加4个城市,然后在div中添加4个城市。我想创建一个新div并添加4个城市,以此类推 以下是html参考文件: <div class="col-lg-3 col-md-3 col-sm-6 col-6"> <p><a href="/property/listings/?city=islamabad" class="fot
<div class="col-lg-3 col-md-3 col-sm-6 col-6">
<p><a href="/property/listings/?city=islamabad" class="fot-text">Islamabad</a></p>
<p><a href="/property/listings/?city=karachi" class="fot-text">Karachi</a></p>
<p><a href="/property/listings/?city=lahore" class="fot-text">Lahore</a></p>
<p><a href="/property/listings/?city=rawalpindi" class="fot-text">Rawalpindi</a></p>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-6">
<p><a href="/property/listings/?city=Abbottabad" class="fot-text">Abbottabad</a></p>
<p><a href="/property/listings/?city=Abdul Hakim" class="fot-text">Abdul Hakim</a></p>
<p><a href="/property/listings/?city=Ahmedpur East" class="fot-text">Ahmedpur East</a></p>
<p><a href="/property/listings/?city=Alipur" class="fot-text">Alipur</a></p>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-6">
<p><a href="/property/listings/?city=Arifwala" class="fot-text">Arifwala</a></p>
<p><a href="/property/listings/?city=Astore" class="fot-text">Astore</a></p>
<p><a href="/property/listings/?city=Attock" class="fot-text">Attock</a></p>
<p><a href="/property/listings/?city=Awaran" class="fot-text">Awaran</a></p>
</div>
我想喜欢这个HTML
我怎样才能做到这一点
以下代码不起作用:
<div class="col-lg-3 col-md-3 col-sm-6 col-6">
{% for city in citys %}
<p><a href="/property/listings/?city={{ city }}" class="fot-text">{{ city }}</a></p>
{% endfor %}
</div>
{城市中城市的%s%}
{%endfor%}
它将所有元素添加到一个div中
如何在该div上添加4项后创建新div您可以使用
forloop.counter
{% for city in citys %}
{% if forloop.first %}
<div class="col-lg-3 col-md-3 col-sm-6 col-6">
<p><a href="/property/listings/?city={{ city }}" class="fot-text">{{ city }}</a></p>
{% elif forloop.counter|divisibleby:"4" and not forloop.last %}
<p><a href="/property/listings/?city={{ city }}" class="fot-text">{{ city }}</a></p>
</div>
<div class="col-lg-3 col-md-3 col-sm-6 col-6">
{% elif not forloop.last %}
<p><a href="/property/listings/?city={{ city }}" class="fot-text">{{ city }}</a></p>
{% endif %}
{% if forloop.last %}
{% if forloop.first %}
</div>
{% else %}
<p><a href="/property/listings/?city={{ city }}" class="fot-text">{{ city }}</a></p>
</div>
{% endif %}
{% endif %}
{% endfor %}
{citys%中城市的%s}
{%if-forloop.first%}
{%elif forloop.counter |可除数为:“4”而不是forloop.last%}
{%elif不是forloop.last%}
{%endif%}
{%if-forloop.last%}
{%if-forloop.first%}
{%else%}
{%endif%}
{%endif%}
{%endfor%}
试试这个
我会帮助你的
{% for i in '0123'|make_list %}
<div class="col-lg-3 col-md-3 col-sm-6 col-6">
{% for city in citys %}
<p><a href="/property/listings/?city={{ city }}" class="fot-text">{{ city }}</a></p>
{% endfor %}
</div>
{% endfor %}
{'0123'中i的%make_list%}
{城市中城市的%s%}
{%endfor%}
{%endfor%}
无法分析“%”中的剩余部分:“%”确实,我已修复了它,请立即尝试。请在您的else@HamzaMirchi当然,我还更改了处理最后一条记录的逻辑。@HamzaMirchi,很高兴听到这个消息,请注意使用我的上一个编辑版本,其中我添加了先签入elif
,那个forloop.counter
不是最后一个{%elif forloop.counter|divisibleby:“4”而不是forloop.last%}
它只创建了3个div想象一下如果我有100个城市,那会怎么样