4

My Django template location is listed as:

python\Lib\site-packages\django\contrib\auth\templates

In my view I have:

return render_to_response('hello.html', {'name': name})

When I keep the hello.html in this location it's working

python\Lib\site-packages\django\contrib\auth\templates

Though i have kept template location as

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates'),
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },

]

NewProject
    -NewProject
        settings.py
    -signups 
        view.py
             def hello(request):
                 name='vijay'
                 return render_to_response('hello.html',{'name':name})
    templates
        hello.html
user1844634
  • 1,221
  • 2
  • 17
  • 35
  • Instead of saying does not work, why don't you take a good look at the stacktrace to see what it says, if you need help with that add it to the question. – e4c5 Sep 14 '15 at 09:24
  • @user1844634 What is django version? – salmanwahed Sep 14 '15 at 09:31
  • 1
    just print os.path.join(os.path.dirname(__file__), 'templates'), it in setings.py file and checkout on your error page where django try to find out your template folder in your project for more: stackoverflow.com/questions/3038459/django-template-path – GrvTyagi Sep 14 '15 at 13:36

1 Answers1

3

If you're defining TEMPLATES in your settings.py file, TEMPLATE_DIRS won't have any effect. You should move value from TEMPLATE_DIRS into DIRS sub-setting inside TEMPLATES.

Also, os.path.join(os.path.dirname(__file__), 'templates') will generate path to your templates directory relative to settings.py file, but your templates folder is one dir above settings.py. That line should look like this:

os.path.join(os.path.dirname(os.path.dirname(__file__)), 'templates')

Quick explanation: it will take path to settings.py, get it's parent directory path, get that directory parent directory path and glue into it your folder name. So all should look like this:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': (
            os.path.join(os.path.join(os.path.dirname(__file__)), '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',
            ],
        },
    },
]

Actually, path to your project directory it's already defined in default settings.py, so if you're using default one, you can simplify it to:

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',
            ],
        },
    },
]
GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
  • Check twice if your path to templates is correct. You can print it inside your `settings.py` file and run development server via `manage.py`, that will show you full path built by `os.path.join` and `os.path.dirname`. – GwynBleidD Sep 14 '15 at 12:35
  • I have added the folder structure in the main query. in the settings.py i defined TEMPLATES – user1844634 Sep 14 '15 at 13:07