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?