Django 3 : Displaying Hello world! in a template

Louis SanchezAugust 15th 2021, 10:47

Templates are files that will allow us to generate the HTML code returned to the client.

Step 1 : We will create the first template of our application. To do so, we must first edit the file to define the folder that will contain our templates.

import os
# ...
'DIRS': [os.path.join(BASE_DIR, 'myproject', 'templates')],

Step 2 : Now that Django knows where to look for the templates, we will create the first template of the application. To do this, use a file browser and add the index.html file in the templates folder.


    <title>Hello World</title>

    <h1>Hello World</h1>
    <p>Hello world !</p>


Step 3 : We need to change the view to indicate its use. We will modify the file with the following content:

from django.shortcuts import render

def page(request):
    return render(request, 'index.html')

Step 3 : Let's go back to our browser and refresh the page with the F5 key