reza
reza

Reputation: 31

Django TemplateDoesNotExist and BASE_DIRS

I am trying to load a template in django. I created a text file called current_date.html and typed inside the file

"It is now {{current_date}}." 

and put it inside a templates directory

C:\Users\reza\env_mysite\lib\site-packages\django\contrib\admin\templates

Then inside the view I wrote below block of code:

from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse, Http404
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    t = get_template('current_datetime.html')
    html = t.render(Context({'current_date':now}))
    return HttpResponse(html)

and inside urlpatterns I typed:

url(r'^time/$', current_datetime)

In the settings.py file, inside DIRS in typed:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,

and next time I typed

'DIRS': ['C:\Users\reza\env_mysite\lib\site-
packages\django\contrib\admin\templates']

but in both scenarios I received TemplateDoesNotExist error with below details

Request Method: GET
Request URL: http://127.0.0.1:8000/time/

Django Version: 1.11.2
Python Version: 3.6.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Template loader postmortem
Django tried loading these templates, in this order:

Using engine django:
    * django.template.loaders.filesystem.Loader: C:\Users\reza\templates\current_datetime.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: C:\Users\reza\env_mysite\lib\site-packages\django\contrib\admin\templates\current_datetime.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: C:\Users\reza\env_mysite\lib\site-packages\django\contrib\auth\templates\current_datetime.html (Source does not exist)

Could you please let me know what is wrong with my codes

Upvotes: 1

Views: 582

Answers (2)

YellowShark
YellowShark

Reputation: 2269

You said that you placed this new template, current_date.html in the C:\Users\reza\env_mysite\lib\site-packages\django\contrib\admin\templates directory. That is part of the Django package, and should contain two directories, admin/, and registration/. Is it possible that you placed your new template inside of one of those inner folders, like the admin/ folder?

If so, then you should update the call to get_template() in your view so that it looks like this:

def current_datetime(request):
    now = datetime.datetime.now()
    t = get_template('admin/current_datetime.html') # note updated path
    html = t.render(Context({'current_date':now}))
    return HttpResponse(html)

Note that you're not conforming to best practices here, but that's a separate issue really. A couple suggestions would be to shift this new template into your own application's templates/ directory (it's generally not a good idea to make modifications to the installed packages), and you should also use django.shortcuts.render, which reduces the amount of code you have to write/manage:

from django.shortcuts import render

def current_datetime(request):
    now = datetime.datetime.now()
    return render(request, 'admin/current_datetime.html', {'current_date':now})

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599610

You should absolutely not be putting your own templates inside any of Django's directories. Your code should be completely separate.

You should create your own templates directory inside your project and put your template in there; then your first attempt, os.path.join(BASE_DIR,'templates'), would work.

Upvotes: 2

Related Questions