Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Django is one of Python's most popular, trusted, and reliable web development frameworks. With Django, we can quickly create web applications without worrying about installation or dependency problems that we usually find with other frameworks.
Django provides convenience to rendering data in a template. Django templates allow the passing of data from view to template and also provide some limited features of programming such as variables, for loops, etc. Variables in Django are used for passing the data from view to template.
In this article, we will learn about variables in Django Templates. So, let's get started.
Django Templates
As we know, HTML is a static language and can't interpret with the python code as it is a dynamic language, so Django templates act as a bridge between these two and help form dynamic HTML pages.
Django Templates also contain static parts as they are written in the desired HTML, CSS, and Javascript output. With the help of this, the pages can be rendered quickly and efficiently, and the process becomes easy.
Refer to Django Template to learn about Templates in Django in detail.
This article will focus on Variables in Django Templates in detail.
Variables in Django Templates
Variables in Django Templates look like this: {{ variable }}. When the Django template engine encounters a variable, it replaces that variable with the same variable passed in the template engine.
A Variable name must contain the following:-
Any alphabetical letter (a-Z)
Any digit (0-9)
An underscore or a dot (but they must not start with a dot or underscore).
A dot in a variable name has a special meaning during template rendering. It signifies a lookup. Specifically, when the template engine encounters a dot in a variable name, it looks for the following lookups, in this order:
Dictionary lookup. Example: foo["bar"]
Attribute lookup. Example: foo.bar
List-index lookup. Example: foo[bar]
The Template below has two variables {{ first_name }} and {{ brand_name }} with a context of { ‘first_name’: ‘Naman’, ‘brand_name’: ‘Coding Ninjas Studio’ }.
My name is {{ first_name }}, and I am learning from {{ brand_name }}.
The above template will render to:
My name is Naman, and I am learning from Coding Ninjas Studio.
Explanation
In this section, we will learn how to create a Variable and use it in our Django project. For explanation, we will consider a project named Coding Ninjas Studio having an app called app.
Firstly we will create a Django template using variables and basic HTML in app / template.html. This template.html file will be used to render our result in the browser.
My name is {{ first_name }}
<br>
and I am learning from {{ brand_name }}.
After creating the template, we need to create a view to pass our context dictionary to template.html. We will create the view at app/view.py,
After creating the view and template for the Django project, we need to create a url path to map the view created earlier. We will create the url path in a new file app/url.py,
# Import urls from Django
from Django.urls import path
# importing views from view.py
from .view import app_view
urlpatterns = [
path('', app_view),
]
You can also try this code with Online Python Compiler
Now, let's test our application in the browser and see that everything works successfully.
Hooray, we have successfully created our Django Template and used the variables in Django Templates to display our result in the browser by creating a new Django project.
(Source: Giphy)
Frequently Asked Questions
What happens when a variable is not a string? In Django, if the variable is not a string the __str__ method will be used by Django to display the variable; and with the same principle, we can access an object attribute just like we do it in Python.
What is the Django Template? A Django template is a python string or a text document marked up using the Django template language. It uses its syntax to deal with tags, variables, filters, etc.
Key Takeaways
This article taught us how to create Variables in Django Template and use it with the context in the view model file. We also learned about the naming convention of variables in Django Templates.
Don't stop here. Check out the blogs Best Django Books, Top 30 Basic Django Interview Questions: Part 1, and Top 30 Intermediate Django Interview Questions: Part 2.
We hope you found this blog helpful. Liked the blog? Then feel free to upvote and share it.