Use {% for%} and {% if%} in Django template

0

I have 3 videos saved in the Postgres database where I set a star_date and the video only appears in the template when start_date <= date.today()

My problem is in template . I have 3 buttons, one for each video, my idea is that any video that does not have start_date <= date.today() has its button with opacity: 0.3;

But the way I'm doing, when any video is active it receives the button with images/01.png and <a href="0"> and anyone who is inactive receives the button with images/02.png and <a href="">

When active I need to have images/01.png to have <a href="0"> images/02.png have <a href="1"> and images/03.png has <a href="2">

I'm doing the following:

video-list.html:

<div class="numbers-video">
    <!-- MOSTRA VIDEOS ATIVOS -->
    {%for video in videos%}

        {%if video.is_visible%}
            <a href="0">
                    <img src="{% static 'images/01.png' %}" alt="01">
            </a>

        {%endif%}

        <!-- MOSTRA VIDEOS INATIVOS -->
        {%if video.is_visible == False%}
            <a href="">
                <img class="opacity-button" src="{% static 'images/02.png' %}" alt="02">
            </a>
        {%endif%}

    {%endfor%}
</div>

views.py:

# LISTA DOS VIDEOS
def video_list(request, position):

    if 'lead_id' in request.session:
        video = Video.objects.filter(position=position).first()
        videos = Video.objects.order_by("position").all()

        return render(request, 'video-list.html', {'video': video, 'position':position, 'videos': videos})

    return redirect('registrations:create_lead')

models.py:

class Video(models.Model):
    url = models.CharField(max_length=500, verbose_name='Link Embed')
    title = models.CharField(max_length=255, verbose_name='Título')
    description = models.TextField(max_length=1000, verbose_name='Descrição do video')
    start_date = models.DateField(verbose_name='Estará disponível quando')
    position = models.IntegerField(verbose_name='Qual posição')

    @property
    def is_visible(self):
        return self.start_date <= date.today()

Can anyone give me this strength?

    
asked by anonymous 02.10.2018 / 19:22

2 answers

0

I was able to solve the problem as follows:

Before:

video-list:

<div class="numbers-video">
    <!-- MOSTRA VIDEOS ATIVOS -->
    {%for video in videos%}

        {%if video.is_visible%}
            <a href="0">
                    <img src="{% static 'images/01.png' %}" alt="01">
            </a>

        {%endif%}

        <!-- MOSTRA VIDEOS INATIVOS -->
        {%if video.is_visible == False%}
            <a href="">
                <img class="opacity-button" src="{% static 'images/02.png' %}" alt="02">
            </a>
        {%endif%}

    {%endfor%}
</div>

Now:

video-list:

        <div class="numbers-video">
            <!-- MOSTRA VIDEOS ATIVOS -->
            {%for video in videos%}

                {%if video.is_visible%}
                    <a href="{{forloop.counter0}}">
                        <img src="{% static 'images/0' %}{{forloop.counter}}.png" alt="0{{forloop.counter}}">
                    </a>

                {%else%}
                    <a>
                        <img class="opacity-button" src="{% static 'images/0' %}{{forloop.counter}}.png" alt="0{{forloop.counter}}">
                    </a>
                {%endif%}

            {%endfor%}
        </div>

Using the {{forloop.counter}} method, I assign a value according to each list item in the case of {{forloop.counter0}} use 0 because the counter starts with 1, that way I can make my page dynamic

    
02.10.2018 / 20:18
0
      {%if video.is_visible == False%}

Should be

      {%if not video.is_visible %}

Or rather, simply use

      {% else %}
    
02.10.2018 / 19:33