The reason could be that time
and start_time
are not both timezone-aware or both timezone-naive. Display the times with the timezone information to check.
Solution 1 :
Problem :
I am passing in class objects and a variable into my html template in Django
Using Classbased Views, I have
get_context_data(self, **kwargs):
...
context['time'] = datetime.now()
...
return context
I also pass in my objects using
model = Object
and context_object_name = 'obj'
In my html template my comparison looks like this:
{% if obj.start_time > time %}
<h1 style="color:white">ITS WORKING</h1>
{% endif %}
Regardless of whether I use >, <, or ==, the h1 template is never displayed.
I have used {{obj.start_time}} and {{time}} to ensure that there really are values attached to these variables, and I see both of the times displayed on the page. The values themselves work, but for some reason the comparison isn’t working.