Show list items by filtering by date.today ()

0

I have a list that I sort by position in my models.py , however I have the start_date field that I want to use as a validator for the item to be visible or not. But it is not working:

views.py :

from django.shortcuts import render, redirect
from datetime import date
from .models import *
from .forms import *
import requests

def videos_list(request):

    if 'lead_id' in request.session:
        videos = Video.objects.order_by("position").all()
        for video in videos:
            print(video.title)
            if video.start_date < date.today():
                print(True)
                show_video = True
            else:
                print(False)
                show_video = False

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

    return redirect('registrations:create_lead')

console :

Programe ou seja programado - Episódio 01 True Programa ou seja programado - Episódio 02 True Programe ou seja programado - Episódio 03 False

I understood that at the end of the looping it leaves my variable show_video as False and that's why my template is empty

video-list.html :

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div>
        {% for video in videos %}
        {% if show_video %}
        <div>
            <h1>
                {{video.title}}
            </h1>
            <iframe width="560" height="315" src="{{video.url}}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
            <p>
                {{video.description}}
            </p>
        </div>
        {% endif %}
        {% endfor %}
    </div>
</body>
</html>

But I do not understand how to do otherwise, can anyone give me this strength?

    
asked by anonymous 27.09.2018 / 20:32

1 answer

3

You need a show_video attribute related to each Video object that you submit to the template.

If it were not an object-oriented paradigm, you could create a list, with the value of show_video relative to each video, and use zip of Python in its for within the template -

    ...
    show_videos = []
    for video in videos:
        print(video.title)
        show_videos.append(video.start_date < date.today()_  # True ou False

And then you would have to put the logic to go through this list and the list of videos in parallel in the template. Fortunately, this is not necessary.

Because it is OO, and Python accepts more attributes within the objects, you can also simply embed an extra attribute in the videos, before calling the template:

    ... 
    for video in videos:
        print(video.title)
        if video.start_date < date.today():
            video.show_video = True
        else:
            video.show_video = False
    ...
    # e no template:
    {% if video.show_video %}

But better still, Python has properties - which allow properties that can be dynamically computed with code to work, for those who are using the object, as if they were an attribute.

This means you can put the show_video attribute as a property in the video class - it is different from an attribute that is an instance of a% django% - it is not written to the database. And then, you put the same logic that is in your view, right in the model:

from datetime import date

class Video(models.Model):
    ...
    # campos do ORM
    # 
    ...
    @property
    def show_video(self):
          # o valor da expressão já é um boolean, ou seja True ou False:
          return self.start_date < date.today():

Since this is the only operation you are doing in Field of the view, you do not need it anymore - the view is only:

def videos_list(request):

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

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

And in the template, just modify the for as I put above, to get the field if direct of the video object:

    {% if video.show_video %}
    
27.09.2018 / 20:54