Now that you’ve gotten a feel for the Django Template Language (DTL), it’s probably time to explain the basic design philosophy behind the DTL. First and foremost, the limitations to the DTL are intentional.
Django was developed in the high volume, ever-changing environment of an online newsroom. The original creators of Django had a very definite set of philosophies in creating the DTL. These philosophies remain core to Django today. They are:
- Separate logic from presentation – A template system is a tool that controls presentation and presentation-related logic – and that’s it. The template system shouldn’t support functionality that goes beyond this basic goal.
- Discourage redundancy – The majority of dynamic web sites use some sort of common site-wide design – a common header, footer, navigation bar, etc. The Django template system should make it easy to store those elements in a single place, eliminating duplicate code. This is the philosophy behind template inheritance.
- Be decoupled from HTML – The template system shouldn’t be designed so that it only outputs HTML. It should be equally good at generating other text-based formats, or just plain text.
- XML should not be used for template languages – Using an XML engine to parse templates introduces a whole new world of human error in editing templates – and incurs an unacceptable level of overhead in template processing.
- Assume designer competence – The template system shouldn’t be designed so that templates necessarily are displayed nicely in WYSIWYG editors such as Dreamweaver. That is too severe of a limitation and wouldn’t allow the syntax to be as nice as it is. Django expects template authors are comfortable editing HTML directly.
- Treat whitespace obviously – The template system shouldn’t do magic things with whitespace. If a template includes whitespace, the system should treat the whitespace as it treats text – just display it. Any whitespace that’s not in a template tag should be displayed.
- Don’t invent a programming language – The template system intentionally doesn’t allow the following – assignment to variables and advanced logic. The goal is not to invent a programming language. The goal is to offer just enough programming-esque functionality, such as branching and looping, that is essential for making presentation-related decisions. The Django template system recognizes that templates are most often written by designers, not programmers, and therefore should not assume Python knowledge.
- Safety and security – The template system, out of the box, should forbid the inclusion of malicious code – such as commands that delete database records. This is another reason why the template system doesn’t allow arbitrary execution of Python code.
- Extensibility – The template system should recognize that advanced template authors may want to extend its technology. This is the philosophy behind custom template tags and filters.
DTL Philosophy – Concluding Thoughts
Having worked with many different templating systems myself over the years, I whole-heartedly endorse this approach – the DTL and the way it has been designed is one of the major pluses of the Django framework.
When the pressure is on to Get Stuff Done, and you have both designers and programmers trying to communicate and get all the of the last minute tasks done, Django just gets out of the way and lets each team concentrate on what they are good at.
Once you have found this out for yourself through real-life practice, you will find out very quickly why Django really is the “framework for perfectionists with deadlines”.
More than any other component of web applications, template syntax is highly subjective, and programmers’ opinions vary wildly. The fact that Python alone has dozens, if not hundreds, of open source template-language implementations supports this point. Each was likely created because its developer deemed all existing template languages inadequate.
With all this in mind, Django is flexible – it does not require you to use the DTL. All of the most recent versions of Django, including Django 1.11, ship with the popular Jinja2 template engine as well as the DTL to provide developers with options.
Because Django is intended to be a full-stack web framework that provides all the pieces necessary for web developers to be productive, most times it’s more convenient to use the DTL, but it’s not a strict requirement in any sense.