David/Daniel, Thanks a lot for the prompt responses, ill use the method as specified by Daniel.
I had use methods earlier for get_absolute_url and stuff, but didnt realize i could use methods to do anything I wanted... this is nice. Thanks On Nov 25, 4:06 pm, Daniel Roseman <[EMAIL PROTECTED]> wrote: > On Nov 25, 8:55 am, sajal <[EMAIL PROTECTED]> wrote: > > > > > > > > > Hi all i am both a Django and python noob. > > > The thing is I have a database of people and need to show a widget on > > all pages listing out people who are celebrating their birthday today. > > > Using them in each views didnt seem right, specially since later id > > like this portion of the page to be cached so it doesnt seem right. > > > I added a custom context processor with the function :- > > > def borntoday(request): > > date = datetime.date.today() > > return {'borntoday': Person.objects.filter > > (date_of_birth__month=date.month, date_of_birth__day=date.day)} > > > In my template i access this as :- > > > {% if borntoday %} > > <div class="borntoday"> > > <h4>Birthday file - {{ date|date:"jS F" }}</h4> > > <ol> > > {% for person in borntoday %} > > <li><a href="{{ person.get_absolute_url }}">{{ person }}</a></li> > > {% endfor %} > > </ol> > > </div> > > {% endif %} > > > Now Id like to show age of the people along with their names. > > > I have a function which can calculate age callable via calculateAge > > (p.date_of_birth) but how do I make pass this along with the person > > object? > > > I am too new in python, do i need to re-assemble the python list? > > > Could someone point me to some sample code which can add the age into > > the list being passed to the template? > > > BTW is my approach the correct one or am I supposed to tackale this > > problem in a diferent manner? > > > Thanks in advance. > > I would make calculateAge a method on the Person model - or even > better, make a method that's a shortcut to it: > class Person(models.Model): > ... > def get_age(self): > return calculateAge(self.date_of_birth) > > Then in the template you could just do: > > {% for person in borntoday %} > <li><a href="{{ person.get_absolute_url }}">{{ person }} > ({{ person.get_age }})</a></li> > {% endfor %} > > or whatever you want. > > On your question as to whether this is the right approach, there's > nothing definitely wrong with it but I would say this belongs better > in a template tag. In fact you could make the whole template snippet > above into an inclusion tag, then simply insert it in any of your > templates with a single tag. > > -- > DR. --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---