Template Tag

Ashish Sharma
4 min readApr 21, 2018

So ,we learnt about templates in last blog where we came to know about their importance and use. As it is impractical to just return a single line of text, we can make use of templates to return whole html pages. Also, I told you to work with our previous project ( https://medium.com/@sharma.ashish.1996/a-small-project-in-url-mappings-9af69b0560fd )and include more data along with some images. You could have included lists or paragraphs, but in order to include images and css, we have to know about static files, which we have not done yet. We will discuss about static files next week, for today let’s start with template tags!! You can watch the improved version of the project using list tags in html from the following link:

https://www.facebook.com/ashishsharma260696/videos/243404192898825/

Template tags are basically used to insert the data into our html with the help of Django. So, we can actually play with our html by using Django!! The data is included into the html with the help of our own dictionary. In python, we can make a dictionary which will have a key-value pair inside {}. So, based on our key values, the pair text would be inserted into our html. For this, let the name of our project and application be “project” and “app” respectively. We already know how to add the template directory in our settings.py, so first that should be done. Now, we can jump into our html.

The syntax for template tag is as follows:

{{ name_of_the_tag}}

We will include this in body tag and then we will connect this html to our views.py along with this template tag. So, now our html would look like this:

Let the name of our tag be “firsttemplate”.

As we already did, we have to now return this template with the help of render command. We discussed only two parameters in our render function/command which were the request and template. But render consists of a third parameter too, which is our dictionary. So, now we have to create a dictionary in our views.py. Let the following be our dictionary:

dict={‘firsttemplate’:”I am from dictionary”}

We can notice that the key value is same as our template tag. It simply means that whenever that key is encountered, we have to include the value corresponding to that key into our html. We can relate it to some extent to typedef in C, like as soon as the alias for the name is encountered, it is replaced with original content. So, now we can return our template with the help of the render function with three arguments. So our views.py will now have the following content:

The dict is called with the help of context.

We already know how to add our url to the urlpatterns[], so that can be done as following:

So, if everything is done as per said, we will not have any difficulty in running our server. Now, if we visit the specified url, we will get our page as shown:

The page is zoomed in!!!

We can see that the text from our dictionary is now inserted into our html with the help of our template tag. So that is the beauty of Django, we are now able to write in our html using our dictionary which is specified in views.py of our project. We will be discussing about the static files in the coming blog, till then stay safe and keep coding…

Thank you for reading, if you liked the article please click on the clap button and show your support. You can clap more than once, just keep pressing the icon!!!

--

--