0

I am trying to render a template inside a function. Below is my settings.py

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

]

and views.py

from django.shortcuts import render_to_response

def index(request):
   'Display map'
    return render_to_response('waypoints\index.html')

And below is my error when i run the server.Please help me.

    http://dpaste.com/0SZAR4H
harinish
  • 261
  • 3
  • 5
  • 17
  • Is this your complete error? Your url seems invalid unless you have deliberately declared that url `waypoints\index.html` should be `waypoints/index.html` – Arpit Goyal Aug 04 '15 at 12:10

3 Answers3

0

The template you are trying to access is according to the path:

waypoints\index.html

I think it should rather be

waypoints/index.html
Arpit Goyal
  • 2,212
  • 11
  • 31
  • are you on windows? what is your django version? – Arpit Goyal Aug 04 '15 at 12:15
  • Yes am in windows and am using django 1.8.3. And also I made a folder named templates in my project and new folder inside templates waypoints and then "index.html". – harinish Aug 04 '15 at 12:17
  • This is basically a path error. The template path you are specifying is not matching your directory path. Rather than hard coding your directory structure, try this: `TEMPALTE_DIRS = [os.path.join(BASE_DIR, "templates")]`. Here `BASE_DIR` is the variable as in your default settings file and `templates` is the directory where your template resides. – Arpit Goyal Aug 04 '15 at 12:20
  • It will recurse over all the directories and files inside that path. – Arpit Goyal Aug 04 '15 at 12:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85111/discussion-between-arpit-goyal-and-harinish). – Arpit Goyal Aug 04 '15 at 12:30
  • Your templates dir includes waypoints. So maybe you are pointing to: E:\Django\googlemaps\templates\waypoints\waypoints\index.html? Is that what you want? – joel goldstick Aug 04 '15 at 12:57
0

The problem is that \t in \templates is being treated as a tab character.

The easiest fix is to always use forward slashes. This works, even on Windows.

'DIRS': ['E:/Django/googlemaps/templates/waypoints'],

Your other options are:

  • use two backslashes, i.e. \\t
  • use the r'' prefix i.e. r'E:\Django\googlemaps\templates\waypoints'.
Alasdair
  • 298,606
  • 55
  • 578
  • 516
0

I just replaced settings.py shown above by

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(os.getcwd(), "templates"),

) It worked fine.

But i would like to know what makes the difference.

harinish
  • 261
  • 3
  • 5
  • 17