top of page

Django Part IV- Django Templates

In this blog we will discuss django templates. lets see. if you are begineer you can check out the this blog Djnaog click here.

What is templates

Django templates are the most important part of the django mvt structure. It is basically a web page file such as html, css, or javascript. The framework of django generates and handles the web pages that are visible to the end user. Django is a backend function, so in order to provide the frontend and provide a layout to the website used templates. There are two ways to add the templates to the website depending on our needs.

Now we will create a template folder that contains over the entire project. Generally App-level templates are used in big projects or if we want to provide a different layout to each component of our webpage

Basic Example :

Code :

        # Template backend to be used, For example Jinja
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # Directories for templates
        'DIRS': [],
        'APP_DIRS': True,
        # options to configure
        'OPTIONS': {
            'context_processors': [

Django templates

Now we will show an example of how to use templates in django. Templates show static data as well as from the different databases connected to the application.

Creating view in my_blog_site/

Rendering templates

# create a function
def sample_view(request):
    # create a dictionary to pass
    # data to the template
    context ={
        "list":[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    # return response with template and context
    return render(request, "sample.html", context)

Mapping the url to render the view

Code :

from django.urls import path
# importing views from
from .views import sample_view
urlpatterns = [
    path('', sample_view),

Now we will create the templates



<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <h1>Welcome to Codersarts.</h1>
<p> Data  is {{  data }}</p>
    <h4>List is </h4>
    {% for i in list %}
    <li>{{ i }}</li>
    {% endfor %}

Django template language

Django Templates provides this as one of the most significant features. Django templates are text documents or Python strings that have been marked up with the Django template language. The template engine recognises and interprets some constructs. Variables and tags are the most important. We utilised it as a tag, just like we did with the loop in the previous example. We can also use other conditions like if, else, if-else, empty, and so on. Variables, Tags, Filters, and Comments are the core features of the Django Template language.

The context, which is a dict-like object mapping keys to values, outputs a value to variables. Using Django Template variables, we can retrieve the context object we sent from the view in the template.

Code syntax

{{ variable_name }}

For example :

My first name is {{ first_name }}. My last name is {{ last_name }}.

With a context of {‘first_name’: hritik, ‘last_name’: roshan}, this template renders to:

How Codersarts can Help you in Django ?

Codersarts provide:

  • Django Assignment help

  • Django Error Resolving Help

  • Mentorship in Django from Experts

  • Dajngo Development Project

If you are looking for any kind of Help in Django Contact us

bottom of page