8

I am obtaining the path of template using

paymenthtml = os.path.join(os.path.dirname(__file__), 'template\\payment.html')

and calling it in another application where paymenthtml gets copied to payment_template

return render_to_response(self.payment_template, self.context, RequestContext(self.request))

But I get error

TemplateDoesNotExist at /test-payment-url/

E:\testapp\template\payment.html

Why is the error coming?

Edit : I have made following change in settings.py and it is able to find the template, but i cannot hardcode the path in production, any clue?

TEMPLATE_DIRS = ("E:/testapp" )
Community
  • 1
  • 1
dhaval
  • 2,368
  • 9
  • 35
  • 60

4 Answers4

23

It seems like Django will only load templates if they're in a directory you define in TEMPLATE_DIRS, even if they exist elsewhere.

Try this in settings.py:

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
# Other settings...
TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, "templates"),
)

and then in the view:

return render_to_response("payment.html", self.context, RequestContext(self.request))
# or
return render_to_response("subdir/payment.html", self.context, RequestContext(self.request))

This would render either E:\path\to\project\templates\payment.html or E:\path\to\project\templates\subdir\payment.html. The point is that they're inside of the directory we specified in settings.py.

John Debs
  • 3,714
  • 2
  • 25
  • 35
  • 5
    This is a solid approach, but I wanted to add a little info about how Django loads templates. It will look in directories listed in the TEMPLATE_DIRS variable, in the order in which they are listed. The first match it finds will be used. After that, Django will look in the various app modules under app.templates and load from there. The 'cascade' style loading is very handy for selectively replacing templates from reusable apps, etc. – shawnr Dec 24 '09 at 17:35
11

By the way: A tricky thing is, that django throws TemplateDoesNotExist even if the rendered template includes a template that doesn't exist - {% include "some/template.html" %}... this knowledge has cost me some time and nerves.

pcv
  • 2,121
  • 21
  • 25
2

i do not have a django here, but i think you should use / instead of \\ ?

python helps you about the slashes across OSes

joetsuihk
  • 536
  • 1
  • 3
  • 14
1

Are you certain that this file exists on your system?

E:\testapp\template\payment.html

This error message is pretty straightforward and is seen when Django tries to find your template file by path on the file system and cannot see it.

If the file does exist the next step would be to check the permissions on that file and the directories to ensure that this is not a permissions issue. If your E: drive is a mapped network drive of some network share then you also need to check sharing permissions as well.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 1
    yes the file opens directly in browser and no its not permission issues , its obvious but not working and therefore had to post for help – dhaval Dec 24 '09 at 06:57