Render_to_response gives TemplateDoesNotExist

I get the template path using

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

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

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

But I get an error

TemplateDoesNotExist at / test-payment-url /

E: \ TestApp \ template \ payment.html

Why does an error occur?

Edit: I made the following change in settings.py, and it can find the template, but I can’t hardcode the path during production, any hint?

 TEMPLATE_DIRS = ("E:/testapp" ) 
+8
python django django-templates
Dec 24 '09 at 6:46
source share
4 answers

It seems that Django will only load templates if they are in the directory that 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 will create either E:\path\to\project\templates\payment.html or E:\path\to\project\templates\subdir\payment.html . The fact is that they are located inside the directory specified in the settings.py parameters.

+21
Dec 24 '09 at 8:08
source share
By the way, the hardest part is that django throws out a TemplateDoesNotExist , even if the processed template contains a template that does not exist - {% include "some/template.html" %} ... this knowledge took me some time and nerves.
+11
Jan 09 2018-11-11T00:
source share

I don't have django here, but I think you should use / instead of \\?

python helps you slash on operating systems

+2
Dec 24 '09 at 6:50
source share

Are you sure this file exists on your system?

E:\testapp\template\payment.html

This error message is pretty simple and can be seen when Django tries to find your template file by the path in the file system and cannot see it.

If the file exists, the next step is to check permissions on this file and directories to ensure that this is not a problem. If your E: drive is a mapped network drive of a network share, you also need to check the permissions.

+1
Dec 24 '09 at 6:50
source share



All Articles