Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Django Templates

django-templates
create-django-templates
django-template

DTL – Django Template Language
Using Django Templates, a Front-end developer does not need to learn python and a back-end programmer doesn’t need to know HTML.

A Front end developer can simply leave HTML comments(wherever he wants DB and other information from Django). Later a programmer can simply replace them with a Template language – known as the Django Templates Language (DTL)

Thus DTL is one of the template languages used to embed Django/python codes in HTML files.

DTL has an advantage over others due to its

  • Simplicity
  • Easy to learn the syntax
  • extensible Basic Syntax of the Django Template Language (DTL) DTL syntax is similar to python and is very easy to learn. It is further divided into 3 types
  1. Template tags These template tags do something. This sentence might be hard to understand but you will get an idea after seeing the examples, so don’t worry !!
Note : A template tag is enclosed by {% and %}. Some examples are:
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

Image description
Now if we go to the settings.py, there will a TEMPLATES option as shown above. Now the important thing here is the APP_DIRS

'APP_DIRS':True
Enter fullscreen mode Exit fullscreen mode

What this line means is that Django will search for templates/HTML files in a folder called templates.

That means we have to make a templates folder in our Django app and save all the HTML files there.

Image description

Namespacing the Template

When we load a particular template file, Django goes through each app listed in the INSTALLED_APPS in the settings.py. It does so in a top to bottom order and loads the first file it gets with that name.

We might have more than one app in our project, and also there can be two HTML files with the same name in more than one app.

Suppose you have a Books app and a Pens app. Both have an index.html file in their templates folder.

If you try to include index.html in your views.py for the Books app, Django might end up loading the wrong index.html file.

This happens because, as mentioned earlier, it loads the first instance of the file from the INSTALLED_APPS list.

And it can lead to problems. To avoid this, we use namespacing in the template folders.

What I mean by that is to add another folder with the app name inside the template folder.

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Now lets us make a simple basic.html file, which adds “Hello Viewer” to all its webpages.

The syntax used to write the file is:

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Top comments (0)