1

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.

diogenes
  • 1,865
  • 3
  • 24
  • 51

1 Answers1

3

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()
VMatić
  • 996
  • 2
  • 10
  • 18
  • what about adding a custom tag so I can use in more places with different models? Or change the days? – diogenes Nov 21 '17 at 05:25
  • You should consider whether in your case it makes the most sense to do this calculation in your view layer or your model layer. The template should be considered to be very "dumb" in that it should not be calculating things. – VMatić Nov 21 '17 at 05:32