最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Django Template rows with random numbers of columns 1,2 or 3 - Stack Overflow

programmeradmin1浏览0评论

I have a model Article. Now I want to present them in in rows. Each row should be 100% width and contain randomsly 1, 2 or 3 columns. If first column is 2/3 the next should be 1/3. If first is 1/2, next is 1/2

All possible columns are then: 1/1 1/2, 1/2 1/3, 1/3, 1/3 1/3, 2/3 2/3, 1/3

I have been playing with template tags to be able to forward a (random) variable value into the template and I use the divisibleby to break after 3 columns.

Played with CSS gris as well as Flexbox and HTML/CSS. But not able to get the result I want. I realize that I definetly not understand flexbox good enough and still working on it. But it seems to be the better option? Any help appreciated

random_numbers.py
    @register.simple_tag
    def flxgrw():
        flxgrw = random.randint(1, 2)
        return flxgrw


grid.html
<style>
.flexcontainer {
  display: flex;
  flex-wrap: wrap;
}

.flexitem {
  border: 1px solid red;
  padding: 5px;
}

.break {
  flex-basis: 100%;
  height: 0;
}

</style>

<div class="flexcontainer">
{% for article in article_list %}
  <div class="flexitem" style="flex-grow: {% flxgrw as flx %}{{flx}};">
    <h4>{{ article.title }}</h4>
  </div>

  {% if forloop.counter|divisibleby:3 %}
    <div class="break">
  {% endif %}
  {% if forloop.counter|divisibleby:3 %}
    </div>
  {% endif %} 

{% endfor %}


</div>

This is the result. Not quite what I need:

发布评论

评论列表(0)

  1. 暂无评论