Andrew Welch Andrew Welch - 1 year ago 78
Python Question

What is the path for TEMPLATE_DIRS in django when using virtualenv

I am using virtualenv and I want to know what the

should be, for example if I make a templates folder in the root of my project folder.

Answer Source

You need to specify the absolute path to your template folder. Always use forward slashes, even on Windows.

For example, if your project folder is "/home/djangouser/projects/myproject" (Linux) or 'C:\projects\myproject\' (Windows), your TEMPLATE_DIRS looks like this:

    # for Linux

    # or for Windows; use forward slashes!

Alternatively you can use the specified PROJECT_ROOT variable and generate the absolute path by joining it with the relative path to your template folder. This has the advantage that you only need to change your PROJECT_ROOT, if you copy the project to a different location. You need to import the os module to make it work:

# add at the beginning of
import os

# ...

    os.path.join(PROJECT_ROOT, 'templates/'),