jasongonzales
jasongonzales

Reputation: 1597

How to find django timesince greater than an integer

In my templates I need to know if the 'timesince' a given variable is greater than another variable. If it is greater than for that row I add a class.

Can't figure out a way to compare these in the template and comparing them in the view seems equally complicated since I am not sure how I could loop over them, then pass that information reliably to the template to add a class.

Any ideas?

Upvotes: 0

Views: 292

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174614

You can't do this with timesince, you'll have to do this either with a custom tag or in your view.

I would recommend to do this in your view:

def someview(request):
   objs = Some.object.filter()
   ctx = {}
   ctx['objs'] = []  # hold your objects
   for i in objs:
      ctx['objs'].append((i,i.date_field < someother_obj.date_field))

   return render(request, 'template.html', ctx)

In your template:

{% for obj,flag in objs %}
    <tr><td {% if flag %}class="marked"{% endif %}>{{ obj }}</td></tr>
{% endfor %}

If you want to do it with a custom tag, create a template to show the row and then create an inclusion tag which has your comparison logic.

Upvotes: 1

Related Questions