Tuesday, 13 July 2021

*Episode 7* DJANGO (Template System of Django)

 


Template System


    In Django, it is possible for us to separate HTML and Python. The Python forms the views while our HTML will form the template. For these two to be linked together, we have to rely on the Django template language and the render function.


  •  The Render Function :-

     This function takes in three parameters. These include the following:

  • Request- This is our initial request.

  • Path to our template- This represents the path relative to the option “TEMPLATE_DIRS” in the variable “settings.py” of the project.

  • Dictionary of parameters- This is a dictionary that has all of the variables that are contained in a template. You can choose to declare it or you can use “locals()” for the purpose of passing all the local variables that have been declared in the view.

  •  Django Template Language (DTL) :-

     The template engine for Django provides you with a mini-language for the definition of the user-facing layer of your application.

  •  How to Display Variables :-

     In Django, a variable looks like this:

{{variable}}

     The template works by replacing the above with the name of the variable that has been passed by our view as our third parameter of the function “render”. Suppose that we need our file “hello.html” to display the current date. To do so, the file can be modified to the following:

<html>
<body>
Hello there!!!<p>Today is on {{day}}</p>
</body>
</html>

    The view should also be changed to the following:

def hello(request):
day = datetime.datetime.now().date()
return render(request, “hello.html”, {“today” : day})

     You can then try to access the file “URL/myapplication/hello” and you will observe the following result:

Hello there!!!
Today is on Nov. 11, 2016

     That’s it. As most of you might have noticed, in case a particular variable is not a string but the Django needs to display it, it uses the method “__str__”. With this principle, one can access an attribute of an object in the same way.

  •  Filters :-

     Filters are useful as they can assist you to display variables during the display time. They
have a structure which looks like this:

{{var|filters}}

     Consider the filters given below:

  •  {{string|truncatewords:70}}- With this filter, a string will be truncated, meaning that you will only see the first 70 words.
  •  {{string|lower}}- This filter will convert a string to lowercase.
  •  {{string|escape|linebreaks}}- This will escape the contents of a string, and convertyour line breaks into tags.

     It is also possible to set the default for a particular variable.

  •  Tags :-

     With tags, you can perform operations such as for loop, if condition, template inheritance
and others.

  •  Tag if :-

    In Django, you can use the various versions of the “if” statement as it is done in Python.
    This is shown in the code given below:

<html>
<body>
Hello there!!!<p>Today is on{{day}}</p>
We are on
{% if day.day == 1 %}
the first day of the month.
{% elif day == 30 %}
the last day of the month.
{% else %}
I am not aware.
{%endif%}
</body>
</html>

     In the template given above, the current date will be displayed.

  •  Tag for :-

     This tag works in the same way as the “for” in Python. We now need to change the hello view so that it can transmit a list to the template. This is shown below:

def hello(request):
day = datetime.datetime.now().date()
days = [‘Mon’, ‘Tue’, ‘Wed’, ‘Thu’, ‘Fri’, ‘Sat’, ‘Sun’]
return render(request, “hello.html”, {“today” : day, “days_of_week” : days})

     Here is the template for using the tag for displaying the list:

<html>
<body>
Hello there!!!<p>Today is on {{day}}</p>
We are on
{% if day.day == 1 %}
the first day of the month.
{% elif day == 30 %}
the last day of the month.
{% else %}
I am not aware.
{%endif%}
<p>
{% for days in days_of_week %}
{{day}}
</p>
{% endfor %}
</body>
</html>

     The above code should give us the days of the week as we have specified, together with the messages that we have specified.

  •  Block and Extend Tags :-

     A template system is not complete without the template inheritance. This means that during the process of development of a template, we should have the main template with
loopholes and the child templates should be designed to fill those loopholes according to what the developer needs. This works through the same mechanism in which a page can need a special css for a particular tab.

     Consider the code given below for the template “main.html”:

<html>
<head>
<title>
{% block title %}Page Title{% endblock %}
</title>
</head>
<body>
{% block content %}
Content of the Body
{% endblock %}
</body>
</html>

     Our aim is to change the template “hello.html” so that it inherits from the template given above. Here is the code for this template:

{% extends “main.html” %}
{% block title %}The Hello Page{% endblock %}
{% block content %}
Hello there!!!<p>Today is on{{day}}</p>
We are on
{% if day.day == 1 %}
the first day of the month.
{% elif day == 30 %}
the last day of the month.
{% else %}
I am not aware.
{%endif%}
<p>
{% for days in days_of_week %}
{{days}}
</p>
{% endfor %}
{% endblock %}


👈Episode 6(D).                                          Episode 8(D)👉
Share This Post

PRINT THIS POST

No comments:

Post a Comment

If you have any doubts. Please let me know.

Featured post

*Episode 1* MCQ for Govt. Job/ Private Job/ MNCs

  Topic:- One Word Substitution 1) Especially skilled in storytelling  Answer:- Raconteur 2) Fear of loneliness Answer:- Eremophobia  3) Usa...