Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The template engine is intentionally designed so you have to move any major logic or 'business rules' to the view and out of the template. This can be a different style than other web languages in the beginning, but trust me you will be thankful in 6 months when you are adding a new feature that you don't have to touch the templates (or not much).


I think in most cases when you add a feature you end up touching everything in each of the layers.

It's only when you want to change something that it's possible that you might just be restricted to one layer.

I think the idea of the template is that you have one template and many views, and possibly that a template can be created of many different smaller templates, e.g. header, footer, sidebar.

Here is an example of something that I wanted to do with a very specific template that wouldn't be used on any other page.

My model gives distance in meters, for each of the objects and I wanted to display it in miles. So I had to add a new method to the model dist_miles. Instead of just saying round(cinema.dist / 1600, 2) in the template.

I've only been using django for a week now so its quite possible that I'm going about it all wrong.


Yep, all wrong :) You want a custom filter: http://www.djangoproject.com/documentation/templates_python/


Other types of complexity can be handled in custom template tags too.

The easy way to look at them is you could write your own 'filter' or 'count' or any of the tags you see.

However, most of your needs can be met by the built in filters.




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: