Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Customizing Django Templates Building Custom Tags DIY Custom Tags

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

Why is the Django template syntax so different from Python?

For example:

{{ course.created_at|date:"m/d/y" }}
  • filter gets chained on with a pipe rather than dot
  • arguments to the filter go after a colon rather than inside parentheses

Are there functional or historical reasons for this? Just curious.

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

One reason for a different Django filter syntax is to differentiate the functionality to access attributes and bound methods to the Python object in the variable and the Django template functionality. The Django filters run on the output of the Python object methods and attributes.

In the docs for writing custom filters, besides the filtered object, the filter take a single argument.

The colon (:) indicates whether the filter takes an argument. The reasons behind it aren't clear, but to me, by not using parens it makes it clearly not a regular python function.

jason chan
jason chan
31,009 Points

That's how frameworks work. It's the same for javascript - angularjs, php - laravel has similar syntax

{{ $var }}