I need to check if the variable texts
is defined or not in index.html
.
If the variable is defined and not empty then I should render the loop.
This is what worked for my use case in my Django app:
I needed to pass a queryset as context to an html template and display the block only if the queryset had values
Queryset:
events = Event.objects.filter(schedule_end__gte=date.today()).order_by('-created_at')
Passed context dictionary as follows:
{ "events" : events }
HTML template
{% if events %}
Upcoming Events
{% for event in events %}
{{ event.title }}
{% endfor %}
{% endif %}