Re: ifequal and DateQuerySet objects
On Feb 3, 10:03 am, bradwrote: > You could use the built-in __str__ method on now.year. > In [11]: now.year.__str__() > Out[11]: '2009' > > In [12]: now.year.__str__() == '2009' > Out[12]: True > > In your template you'd do something like this: > {% ifequal year month.year.__str__ %} Doh! I didn't even think about the built-in methods being available in templates. Thanks! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: ifequal and DateQuerySet objects
On Feb 2, 6:37 pm, Bret Wwrote: > I've run into a problem with ifequal in one of my templates. > > I'm using date-based generic view to display all objects for a given > year. > urls.py: > (r'^taken/(?P\d{4})/$' ... > > Part of the extra_context I pass is a DateQuerySet object: > months = Photo.objects.dates('date_taken', 'month', order='DESC') > > In my template, I have the following: > {% for month in months %} > {% ifequal year month.year %} > ... > > It always evaluates to false. > > I know this is because Python won't handle the casting: > > > > >>> from datetime import datetime > >>> now = datetime.now() > >>> now.year == 2009 > True > >>> now.year == "2009" > False > > So, is it possible to handle this comparison in the template? You could use the built-in __str__ method on now.year. In [11]: now.year.__str__() Out[11]: '2009' In [12]: now.year.__str__() == '2009' Out[12]: True In your template you'd do something like this: {% ifequal year month.year.__str__ %} --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
ifequal and DateQuerySet objects
I've run into a problem with ifequal in one of my templates. I'm using date-based generic view to display all objects for a given year. urls.py: (r'^taken/(?P\d{4})/$' ... Part of the extra_context I pass is a DateQuerySet object: months = Photo.objects.dates('date_taken', 'month', order='DESC') In my template, I have the following: {% for month in months %} {% ifequal year month.year %} ... It always evaluates to false. I know this is because Python won't handle the casting: >>> from datetime import datetime >>> now = datetime.now() >>> now.year == 2009 True >>> now.year == "2009" False >>> So, is it possible to handle this comparison in the template? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---