3

I've deployed an app on GAE and am trying to incorporate Django templates. The app worked before I templatized the HTML but it seems the server can't find the right templates. I've looked at other solutions on the web: most of them use a settings file which I don't use since I'd rather have a few lines inline than maintain a separate file.

Here's what my main.py looks like right now:

import webapp2
import database
import os
from django.template import Context, Template, loader
from django.conf import settings

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

#Define template loaders used to load HTML templates
TEMPLATE_LOADERS_STRING = (
'django.template.loaders.filesystem.loader',
'django.template.loaders.app_directories.loader',
) 

#Since this is a stand alone platform, set Django settings here
template_directory = os.path.join(PROJECT_PATH, 'Templates')
settings.configure(DEBUG = True, TEMPLATE_DEBUG = True, TEMPLATE_DIRS = template_directory))

class MainHandler(webapp2.RequestHandler):
    def get(self):   
        #some code here

        t = loader.get_template('main.html')
        c = Context ({"data_entries": database.Music.query()})
        MainPageHTML = t.render(c)  
        self.response.write(MainPageHTML)

app = webapp2.WSGIApplication([('/', MainHandler)], debug=True)

If I change t = loader.get_template('main.html') to the hardcoded absolute path then the template is found.

The error message I get from the server is:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "D:\Jawad\JRJ\main.py", line 43, in get
    t = loader.get_template('main.html')
  File "C:\Program Files (x86)\Google\google_appengine\lib\django-1.5\django\template\loader.py", line 146, in get_template
    template, origin = find_template(template_name)
  File "C:\Program Files (x86)\Google\google_appengine\lib\django-1.5\django\template\loader.py", line 139, in find_template
    raise TemplateDoesNotExist(name)
TemplateDoesNotExist: main.html

Any ideas?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Jay
  • 31
  • 2
  • You can check: http://stackoverflow.com/questions/7353355/templatedoesnotexist-on-python-app-engine-django-1-2-while-template-rendering-ab – Hieu Nguyen Jul 21 '13 at 11:17

0 Answers0