0

To be able to find the template file in django, everybody said you should amend the settings.py like this:

TEMPLATES = [{
    'DIRS': [os.path.join(BASE_DIR, 'templates')]
}]

but I found that this also works:

TEMPLATES = [{
    'DIRS': ['templates'],
}]

My question is what is the difference between them. Why does no one recommend 'DIRS': ['templates']?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130

1 Answers1

1

If you pass a relative path like 'templates' to the DIRS setting, it will be interpreted as relative to the current directory, e.g. the directory you're in when running ./manage.py runserver.

If you try to run the server from a different directory, then this path will probably point to a non-existing directory and your page won't render.

If you pass an absolute path instead, then your app will work properly regardless of the working directory.


Now, How to get the absolute path? Take the absolute path of the project (BASE_DIR) and add templates at the end - this is what os.path.join does for you.

Kos
  • 70,399
  • 25
  • 169
  • 233