I made custom template filter to check type of object.
I thought it will pass only the name of type like ‘int’ or ‘str’.
But it’s not working like that.
- Information
theschedules
in the template contains one integer and 7 objects from mySchedule
model
templatetags/get_type.py
from django import template
register = template.Library()
@register.filter
def get_type(object):
return type(object)
Template HTML
<!DOCTYPE html>
{% extends "main/base.html" %}
{% load static %}
{% load get_type %}
{% block content %}
<table class="table">
<thead>
<tr>
<th scope="col">Period</th>
{% for date in dates_of_week %}
<th scope="col">{{ date|date:"Y m d D" }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for object in object_list %}
<tr>
{% for schedules in object %}
{% if schedules|get_type == "int" %}
<th scope="row">{{ schedules }}</td>
{% else %}
<td>{{ schedules.user }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
So this template doesn’t work like what I expected, but if I change {% if schedules|get_type == "int" %}
to {% if schedules|get_type == 1|get_type %}
, of course it works well.
I want this work as what I expect.
Can you tell me how to fix this?