Note: If you’re using a newer Django version, your template paths will look differently than shown in the video lesson because Django switched to using the pathlib
module. For a quick overview, read about Django with pathlib
below, or a more detailed discussion in the lesson comments.
In this lesson, you’re going to create a template for your Django app so you won’t have to paste all your HTML directly into your views. Django has already provided you with the import statement you’re going to need for this:
from django.shortcuts import render
Now that you have render()
, use it in your function with the template name 'projects/index.html'
:
# Create your views here.
def project_list(request):
return render(request, 'projects/index.html')
Django With pathlib
Newer versions of Django use the pathlib
module instead of the os
module:
# portfolio/settings.py
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
You can read over a detailed discussion of the changes in the lesson comments and learn more about pathlib
.
Gascowin on Oct. 19, 2019
May i ask; why is it that in views.py you have
instead of
? My understanding here is that we are passing in the path to the file index.html and since views.py and the templates folder are in the same directory we would need the path ‘templates/projects/…’ instead of ‘projects/…’ Thanks in advance.