What is the best location to put templates in django project?

Viewed 54721

What is the best location to put templates in django project?

9 Answers

DJANGO 1.11

add templates folder where the manage.py exist,which is your base directory. change the DIRS for TEMPLATES as following in your settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},

]

Now to use the template by using the code ,

def home(request):
    return render(request,"index.html",{})

in views.py. this works completely fine for django 1.11

Previous solution didn't work in my case. I used:

TEMPLATE_DIRS = [ os.path.join(os.path.dirname(os.path.realpath(__file__)),"../myapp/templates") ]
Related