3

我已经在 GAE 上部署了一个应用程序,并正在尝试合并 Django 模板。该应用程序在我将 HTML 模板化之前工作,但似乎服务器找不到正确的模板。我查看了网络上的其他解决方案:它们中的大多数使用我不使用的设置文件,因为我宁愿有几行内联而不是维护一个单独的文件。

这是我的 main.py 现在的样子:

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)

如果我更改t = loader.get_template('main.html')为硬编码的绝对路径,则找到模板。

我从服务器收到的错误消息是:

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

有任何想法吗?

4

0 回答 0