amb1s1
amb1s1

Reputation: 2075

How to rotate between 10 differents background colors using the django for tag

I have a django template thatthe page is going to be fill with messages. I want the messages to rotate between 10 colors. So 1:red 2:blue ........10:yellow 1:red . So if I have 20 message you will been seen each color twice.

here is my stream.html

{% extends "index.html" %}
{% load staticfiles %}
{% block content %}
{% for secret in secrets%}

<section id="blue" class="container-fluid text-blue">
  <div class="container">

    <div class="col-md-12 text-center">

      <h3 class="">{{secret.secretmessage}}</h3>

    </div>
  </div> <!-- /container -->
</section>
{% endfor %}
{% endblock %}

Here is my view:

def stream(request):
    h = Secrets.object.all()
    context     =   locals()
    template    =   'stream.html'

    return render_to_response(template, context)

Since I can't declare variable within the template, I'm stuck to find this solution. Thanks in advance

Upvotes: 0

Views: 457

Answers (1)

amb1s1
amb1s1

Reputation: 2075

I did the following and it work perfect:

{% extends "index.html" %}
{% load staticfiles %}
{% block content %}
{% for secret in secrets%}
<tr class="{% cycle "lightgreen" "purple" "darkgreen" "lightblue" "darkblue" "yellow" as rowcolors %}"></tr>
<section id="{{rowcolors}}" class="container-fluid messages">
  <div class="container">

    <div class="col-md-12 text-center">



      <h3 class="">{{secret.secretmessage}}</h3>

    </div>
  </div> <!-- /container -->
</section>

{% endfor %}
{% endblock %}

Thanks Ricardo

Upvotes: 2

Related Questions