I'm building a timetable application and I need to check if there is a certian room available so I want to create a function/template tag/template filter to return me a boolean value which I want to later on pass into Django template if statement.
View function:
def my_view(request):
args = {
'days': Days.objects.all(),
'times': Times.objects.all(),
'rooms': Rooms.objects.all(),
}
return render(request, 'my_page', args)
Template stucture example that I need:
{% for day in days %}
{% for time in times %}
{% for room in rooms %}
{% if MY_TAG {{ day }} {{ time }} KWARG={{ room }} %}
<p>SOMETHING IF TRUE</p>
{% else %}
<p>SOMETHING IF FALSE</p>
{% endif %}
{% endfor %}
{% endfor %}
I've looked on the official Django docs, SO and other sites and forums for these things. I've found a couple of promising code examples that didn't work but gave me the idea of structure concept for my function.
The function that I have so far is working but I can't implement it in Django template.
My function/template tag:
from ..models import Timetables
from django import template
register = template.Library()
@register.simple_tag
def check_availability(day, time, **kwargs):
if variable == 'room':
if Timetables.objects.filter(day=day, time=time, room=kwargs['room']).exists():
return True
else:
return False