Skip to content
Related Articles

Related Articles

Django Templates | Set – 1
  • Last Updated : 26 Aug, 2019

There are two types of web pages – Static and Dynamic pages. Static webpages are those pages whose content is static i.e. they don’t change with time. Every time you open that page, you see the same content. Their content is independent of time, location, user, etc. Dynamic webpages are those pages whose content are generated dynamically i.e. They vary as per location, time, user and on various factors.

What are templates ?

Django framework efficiently handles and generates dynamically HTML web pages which are visible to end-user. Django mainly functions with 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 need.

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

For our current project, we will create a single template directory which will be spread over the entire project for simplicity. 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.

Adding template to project –

Create a template directory in the same directory as our project. In our case, which is geeksforgeeks. So, our directory structure is now,
template-directory
Now, navigate to geeksforgeeks/geeks_site/settings.py.

filter_none

edit
close

play_arrow

link
brightness_4
code

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

chevron_right


In above code, modify



'DIRS': []

as
'DIRS': [os.path.join(BASE_DIR, 'templates')]

Above line links our project to our template directory using the os module. If you print BASE_DIR in your terminal, you will see the directory of your project. For example in my case, it is

/home/ankush/Desktop/Programming/webproject/geeksforgeeks

Now our command will join our BASE_DIR to ‘templates’ and feed it to ‘DIRS’ key of TEMPLATE. Now, we can our save HTML codes in geeksforgeeks/templates directory and can access it from our code.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :