1

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
BokiX
  • 412
  • 2
  • 14
  • Do not put complicated logic in your templates. Your business logic should go in the view. – dgw Jul 07 '23 at 20:22
  • How should I do it then? I need a template that generates a empty timetable where I can fill in some information one of which is in which room will your class be. I need to check if that room is in use at that specific day in that specific time. – BokiX Jul 08 '23 at 09:17

2 Answers2

1

I think you found everything you needed by yourself, you can add the as XXX to store the result of a tag as context variable

{% for day in days %}
    {% for time in times %}
        {% for room in rooms %}
            {% MY_TAG day time room=room as res %}
            {% if res %}
                <p>SOMETHING IF TRUE</p>
            {% else %}
                <p>SOMETHING IF FALSE</p>
            {% endif %}
    {% endfor %}
{% endfor %}

It is very similar to a python function, day and time will be resolved as context value and passed as first and second arguments. room will also be resolved as context value but passed as a keyword argument behind the keyword room

Alombaros
  • 1,280
  • 1
  • 2
  • 17
  • It doesn't work because I cannot put tag in an if statement. That is where I have problems. `{% check_availability day time room=room %}` this is how I can call the function/template tag but I need it to return True/False value so I can display things based on the result. – BokiX Jul 09 '23 at 08:25
  • 1
    I'm sorry I misunderstood your issue. As you found, you can store the result with an as statement – Alombaros Jul 09 '23 at 09:07
1

I've found a partial answer to my question here. I can use the keyword as to store the value that my template tag check_availability returns into a variable called room_available which I can later use in an if statement.

{% check_availability day time room=room as room_available %}
{% if room_available %}
    <p>SOMETHING IF TRUE</p>
{% endif %}
BokiX
  • 412
  • 2
  • 14