How to Set up a Template Directory in Django

In this article, we show how to set up a template directory in Django.
Template directories are very important in Django, because when rendering templates in Django, Django automatically looks for the template directory in the app that you are working in.
So, if you've created an app and you're using the render function to render a template in file in that app's view.py file, Django is automatically going to look for the location in the templates directory in that app.
It's not as simple as just creating the templates directory in the app and expecting Django to find it automatically based on the fact that the directory is named templates.
You have to specify in the settings.py file where the templates directory is located.
And this is done by going in the settings.py file and entering the complete path to the templates directory that is present in the app that you are using.
For instance, let's say you've created called mywebsite. And in this project, you created the app, Articles. And in this Articles app, you created the templates directory.
Let's say that the complete path to this templates directory is, C:\\Users\\David\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts\\mywebsite\\Articles\\templates
You can specify this full path to the templates directory using the above method.
When putting the path to the templates directory in the settings.py file, you go to the DIRS dictionary in the TEMPLATES set. In this DIR, you can specify the above path to the templates directory.
Therefore, the code would be as shown below.
This would give the full path to the templates directory in the Articles app.
However, there is a much better way of doing it.
When working with Django, you should never have to specify the full path beginning from your computer's path.
You should always start paths beginning with the project directory.
For example, if your project is mywebsite, you should start paths beginning at mywebsite. You should have to specify the full path on your computer from where the directory is located.
So, in the settings.py file, there is a variable named BASE_DIR, there by default, which represents the path beginning from the project's root directory (mywebsite for a project named mywebsite). After this, you would just have to specify the path to the templates directory.
In the case of the templates directory located in the Articles app, the path would be BASE_DIR + Articles/templates.
So, how do we translate this into the settings.py file?
It would be the following code shown below.
This makes the line of code a whole lot simpler than specifying the full path, as we did above, which is unnecessary. This is why BASE_DIR is there. So you don't have to find your full computer path.
Any app that you create that will use templates (which should pretty much be every app) should have its own templates directory.
This means that for every templates directory that you create, you have to go to the settings.py file and specify the path to this templates directory, so that Django knows where the templates directory is for each app.
So, if you create a new app named Contact and you create a templates directory in this app, you would have to update the DIRS dictionary in the
TEMPLATES set to the following.
And this how you can set a templates directory in Django.
Related Resources
How to Randomly Select From or Shuffle a List in Python