Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display blog posts in two columns
#1
I want to diplay the list of my blog posts in a row with two columns. So I tried;

{% for post in posts %}
    {% set oddOrEven = cycle(['odd', 'even'], loop.index0) %}
    {% if oddOrEven == 'odd' %}
        <div class="col-md-6">Odd posts</div>
    {% else %}
        <div class="col-md-6">Even posts</div>
    {% endif %}
{% endfor %}
But I get the error:

Invalid block tag on line 18: 'set', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

#18: {% set oddOrEven = cycle(['odd', 'even'], loop.index0) %}

What am I missing or what is the right approach?
Reply
#2
Is this DJango? IF so you need to post in
https://python-forum.io/Forum-Web-Development
for the future. But i can move it.
Recommended Tutorials:
Reply
#3
https://docs.djangoproject.com/en/2.1/re...ins/#cycle Wrote:In some cases you might want to refer to the current value of a cycle without advancing to the next value. To do this, just give the {% cycle %} tag a name, using “as”, like this:

{% cycle 'row1' 'row2' as rowcolors %}
From then on, you can insert the current value of the cycle wherever you’d like in your template by referencing the cycle name as a context variable. If you want to move the cycle to the next value independently of the original cycle tag, you can use another cycle tag and specify the name of the variable. So, the following template:

<tr>
    <td class="{% cycle 'row1' 'row2' as rowcolors %}">...</td>
    <td class="{{ rowcolors }}">...</td>
</tr>
<tr>
    <td class="{% cycle rowcolors %}">...</td>
    <td class="{{ rowcolors }}">...</td>
</tr>

So since you're not actually referring to it multiple times, you could see if this works:
{% for post in posts %}
  {% if cycle("odd", "even") %}
    <div class="col-md-6">Odd posts</div>
  {% else %}
    <div class="col-md-6">Even posts</div>
  {% endif %}
{% endfor %}
That might not work (I don't know Django's template syntax, Tags might not be the same as normal python functions), if it doesn't, the "as variable_name" syntax shown in the docs should be what you're looking for.
Reply
#4
I had to do a little tweak to get the desired result:

{% for post in posts %}
  {% cycle 'odd' 'even' %}
  {% if cycle=='odd' %}
    <div class="col-md-6">Odd posts</div>
  {% else %}
    <div class="col-md-6">Even posts</div>
  {% endif %}
{% endfor %}
The above will introduce 'odd','even' text labels for every div. So I opted for this;

{% for post in posts %}
  {% if forloop.counter|divisibleby:2 %}
    <div class="col-md-6">Odd posts</div>
  {% else %}
    <div class="col-md-6">Even posts</div>
  {% endif %}
{% endfor %}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Scraping Columns with Pandas (Column Entries w/ more than 1 word writes two columns) BrandonKastning 7 3,168 Jan-13-2022, 10:52 PM
Last Post: BrandonKastning
  Post comments to Wordpress Blog SergeyLV 1 2,482 Aug-01-2019, 01:38 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020