[Django] Django development notes (2) Templates

Tzu-Jan Tung
2 min readMay 23, 2022

--

The views.py file in the app’s folder is where to control what the views look like. It works fine in a simple project.

However, try to imagine that when you are developing a huge project, there might be a lot of views needed to be handled. Then, using templates will make your life easier.

Create template

In the FirstApp folder, add a new directory named ‘templates’, and add a new HTML file name ‘hello’ (or whatever you like), which is the template we created.

Then edit what you like in the HTML file. Mine is like this:

Then revise the code in views.py by replacing the

def saysth(request):
return HttpResponse('Hello world!')

to

def saysth(request):
return render(request, 'hello.html')

Then, click on the play button, and go to the link provided.

Render

Render is the function we used above to access the template. The entire code is shown below.

from django.shortcuts import render

def saysth(request):
return render(request, 'hello.html')

Hover on the ‘render’, the shortcuts will appear. As we can see, function render can take several parameters, the first two are required. Thus, right after the request, we put the template’s name to access the template in the views.py.

--

--

Tzu-Jan Tung
Tzu-Jan Tung

Written by Tzu-Jan Tung

Having a BBA degree and a MSc one in Civil Engineering. Trying to develop programming skills which can be useful in Construction field.

No responses yet