[Django]-What is the best way of including common content (for example header script) in django / python web app

5👍

Short answer: No, Python doesn’t perform that way.

Here’s a long one:

A little bit about difference between PHP and Python web development

First of all, include and require are parts of PHP language itself. It means that before processing source code PHP interpreter includes source code from files given as arguments to those functions and then makes interpreting. That’s the inheritance of the main purpose of PHP design (HTML, or, generally, text preprocessing).

Web development in Python is a bit different. Language itself was designed as general purpose programming language, so it doesn’t assume text-processing by default. Code is just executed. That’s why web development is usually done using frameworks (or wsgi modules if you prefer doing everything from scratch.

All that above means that all text manipulations (all output you generate, you generally do manually by creating result text string (or strings list when used with buffering) and giving the output result yourself.

Template languages

That’s kinda messy point from which template languages were born, becoming essential part of every (or almost every) web application written in Python (you can assume something simalar to Smarty in PHP world). There are a lot of them: jinja, jinja2, mako, chameleon … and many others. All of them are different but serve the same purpose (easyfying text generation via providing templates with placeholders for data of basic representation logic).

And here comes the main feature of Python web frameworks: application logic and representation are strictly splitted.

Here we came to the substance of your question:

As all of those template libraries providing text templating facilities are different, there’s no common way to make PHP-like includes that we can say they are general for Python. Each of those tools provide different ways and philosophy of handling, in particular, includes, mainly divided into two mainstream ways:

  • macros- and slots-based (Chameleon)
  • template inheritance-based (Jinja2, Django template language)

All you need is to take one that best fits your needs (whether you like/unlike their syntax, some of them are faster, some are more readable, and so on..) and work with it combining with other tools that provide logic for your application.

Getting closer: About Django

It’s common to use include template tag when you want to include some re-used part of application (e.g. pagination widget), for which you’ve adapted your view context.

But when you have common header for all pages, or another common part, it’s better to use inheritance, that’s achieved in two steps:

  • Basic template is created (containing general header and footer)
  • Specific templates are created via template inheritance feature, in particular, extends template tag in Django

0👍

Python is not PHP!

Django doesn’t allow you to write arbitrary scripts(code) in templates. You should do all the computations/business tasks in views.py (controller) and pass results into template. In template usually you iterate over your result set and display data. In django 1.4 a new assignment tag was added , so you can do

{% get_movie_tickets_for_theater movie theater as tickets %}

but your get_movie_tickets_for_theater will be defined in templatetags python file.

Leave a comment