On Thu, May 20, 2010 at 4:24 AM, shofty <m...@bytejunkie.co.uk> wrote: > thanks for the swift response. > > On May 19, 10:05 pm, Bill Freeman <ke1g...@gmail.com> wrote: >> You could put a method on your basket object which calculates the shipping. >> Then if the basket object is included in the context, you can trivially call >> it. >> > That seems simple enough for me to do. > > >> Or perhaps better, fix up you all_purchases iterable to append it. >> Something like: >> >> def add_shipping(purchaces_iterable): >> for i, p in enumerate(purchases_iterable): >> yield p >> yield Purchase(name="Shipping", >> amount=Decimal(PER_ITEM_SHIPPING)*(i+1)...) >> > This one seems a bit trickier. where would that code live? is that in > the views module for the basket class?
It's flexible. My though was to define it in views.py, along with its free referencee PER_ITEM_SHIPPING, though that could obviously come from a variable or attribute of an object, or out of the settings module. Then the view would call it on the iterable of purchases that you're using now, and pass the result to the template. But you could also make (something like it) a method on a basket object, taking only self as an argument, and querying the original iterable itseld, then pass the basket to the template and let the template say something like: {% for item in basket.with_added_shipping_item %} > > one thing i thought about last night was that i should probably be > doping this a better way. isnt there a way inside django of creating > an object and including it and everything within it(methods etc) with > a proper simple statement? a so i could do something like > > {% basket %} > > and the basket html would be written out. i could also define {% > basket_total %} and each page would have a summary of the basket? > > is this what decorators are for? if not someone tell me the term for > the funtion i'm describing and i'll go away and do the research. im > Be careful that you don't go too far the other way here. Just as logic (and calculation) in the template is to be avoided, not being the province of the designer, presentation in the code is to be avoided because it *IS* the province of the designer. Whatever code implements the iteration over items, you might consider a separate template to render the individual row, which the code accesses using render_to_string, for example. Personally, I don't find iterating over a set of objects each of which is rendered the same way, simply by accessing attributes, to be too much logic in the template. But, yes, you can do such things. Look for writing your own template tag in the docs. Consider an "inclusion" tag, where you really produce the stuff in a separate template, because writing more general template tags is tricky. Yes, it would still be being done in the template language, but your designer doesn't have to see it, they just see {% basket %}. This will require a load tag higher up in the template to load your custom tag library. Note that your tag will have to presume what the name of the variable holding the basket is, unless you pass it as an argument, e.g.; {% basket basket %} (no problem with the names being the same). Another approach, also using a custom tag library, but writing a filter, which is easier to get right than a tag, would result in usage like: {{ basket|basket }} Yet another way to do something about as good as this is again using a method on your basket objects. This would have usage such as {{ basket.items_list_html }} and wouldn't require loading a tag library. Note: in all these cases remember to mark your return string as safe so that the template engine doesn't escape your less than and ampersand characters. > > cheers so far > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@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. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.