Search
Close this search box.

A guide to using templates in django

In Django, we have a few concepts: URLs, Views and Templates (models too, but we’ll talk about them in a future article). You can think of the flow like this:

  1. User requests a particular web page
  2. Request checks urls.py to see if the URL exists & which view it relates to
  3. Looks up that view in views.py
  4. Returns the response using an HTML template

So, let’s look at how we configure all of that. First off, we need to make some changes to our settings.py file as below. Here, I have added my app ‘kodey’ to the list of installed apps.

A bit further down, you will see that i have defined the templates directory, which is simply /templates. This lives under: ProjectName > App Name > Templates.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'kodey',
]
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'DjangoApp.urls'
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates/'],
        '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',
            ],
        },
    },
]

OK cool, we’re all set. Now, let’s say that the user wants to access our homepage. Well, we define a new path in our urls.py file. The empty quotation marks denote that this is the homepage (there is nothing after the root) and when that request is made, it should refer to views.home.

from django.contrib import admin
from django.urls import path
from kodey import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home),
]

Now, if we look at views.home, you will see that we have defined a new function called home into which we pass the users request. As a response, we return the template at home.html. As part of this, we have also passed a dictionary with a variable into it as an example of how all these things connect.

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here
def home(request):
    return render(request, 'kodey/home.html', {'password':'4839'}) #the template from templates folder

So now, we look at our home.html file, we see that it will simply say ‘hello’ and then print the value of the password parameter that we passed in. So when I load the page, it shows hello 4839.

hello
{{ password }}

So you can see that the urls.py points at a view and the view points at a template. We will be using the view functions to create some nice complex logic in upcoming posts, to show how this can be really very powerful.

Share the Post:

Related Posts