Loading, please wait...

A to Z Full Forms and Acronyms

Django - Templates

In this article we will talk about django templates and learn how we can configure it

Being a web framework, Django needs a convenient way to generate HTML dynamically. The most common approach relies on templates. A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.

Templates are the most important part of Django’s MVT Structure. A template in Django is basically written in HTML, CSS, and Javascript in a .html file. Django framework efficiently handles and generates dynamically HTML web pages that are visible to end-user. Django mainly functions with a backend so, in order to provide frontend and provide a layout to our website, we use templates. There are two methods of adding the template to our website depending on our needs.

  1. We can use a single template directory which will be spread over the entire
  2. For each app of our project, we can create a different template

App-level templates are generally used in big projects or in case we want to provide a different layout to each component of our webpage.

To get started with templates you first need to create a template directory and then a subdirectory for each specific app's template

For example, it looks like this...

-> first_project/templates/first_app

the next step is to let Django know of the templates by editing the DIR key inside of the TEMPLATES directory in the settings.py file

however, there is an issue we have to deal with before we do this ..

what we want is that our Django project to be easily transferrable from one computer to another, but the DIR key will require a "hard-core" path so how do we resolve this?

so this is where the power of python comes into play, we can use python's OS Module to dynamically generate the correct file path string, regardless of the computer

we can import os and try out the following :

->print(    file   )

->print(os.path.dirname(   file    ))

we will use this os module to feed the path to the DIR key inside of the TEMPLATES dictionary

once we've done that we can create an HTML file called index.html inside of the templates/first_app directory

inside this HTML file we will insert template tags (also called Django template variables)," its syntax is: {{variable_name}}".

this template variable will allow us to inject content into the HTML directly from Django!

in order to achieve our objective, we will use the render() function and place it into our original index() function inside of our views.py file

inside index() function we defined a  dictionary named my_dict and there we will use the  same template tag variable that we defined in our index.html 

use python manage.py runserver  command to run your server and your output will be similar like this 

we will talk more about templates in upcoming articles till then happy learning ...

A to Z Full Forms and Acronyms

Related Article