diogenes
diogenes

Reputation: 2109

Django timesince less than values

Does the Django "timesince" filter work with less than "<=" values? I can only get it to work with greater than ">=" values.

I only want to show clients created in the past week. this code does not work.

{% for c in clients %}

   {% if c.created|timesince <= '7 days' %}
       <li><a href="">{{ c.name|title }}</a></li>
   {% endif %}

{% endfor %}

thanks.

Upvotes: 1

Views: 657

Answers (1)

VMatić
VMatić

Reputation: 996

Generally you wouldn't want to convert a date to a string for the purposes of doing a date comparison. You'd want to compare the date objects directly. Have a look at this question and the various useful answers: How to compare dates in Django.

In your case i'd recommend adding a property to the model:

from datetime import date, timedelta

@property
def is_recent(self):
    return (self.created + timedelta(days=7)) > date.today()

Upvotes: 3

Related Questions