On Wed, 2007-02-14 at 13:51 -0800, Jonathan Ballet wrote: [...] > The title is a mix of some fields of a house object : for ex., if the > number of rooms is not available, it is not displayed, etc. > (currently, it's a 30 lines function). > He ends up with a title which looks like this : "House for rent, with > 4 rooms - 1000" or "House to buy - 100000" > > So, the question is : what is the "best" way of rendering this title > in a template. > > We found multiple solutions (looping over each "house"s of the list) : > * a get_title() method in the House class which is used like that : > {{ house.get_title }} > * a format_title filter : {{ house|format_title }} > * adding an additionnal "title" attribute for every house objects, > which is set in the view by a function (how ugly) > * ... something else ? > > To my mind, I think using a filter is better, since it allows you to > redefine another format of title, without changing anything into your > model (since the application should be reusable for other projects). > However, I'm not sure if this is the "best" way to do this ...
There's rarely going to be One True Way to do this sort of thing. For my money, having a method that returns the formatted title feels most natural, partly because it's also available outside of templates. But filters are often used as ways to apply external functions to objects in Django, so if your bent is to write this as an external function, rather than a class method, a filter would also work. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---