Django in Google App Engine - Python 2.7



我正在将一些GAE应用程序从Python 2.5迁移到2.7。将Django模板(任何版本)导入到这个版本的Python中似乎要困难得多。我按照谷歌的指示去T,在网上搜索寻求帮助,但最终失败了。所以这是我尝试过的,我想知道你们中是否有人能够帮助我!提前谢谢。

在 app.yaml 中:

libraries:
- name: django
  version: "1.2"

在main.yaml中:

import os
# specify the name of your settings module
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django.core.handlers.wsgi
app = django.core.handlers.wsgi.WSGIHandler()

主类:

class Main(webapp2.RequestHandler):
  def get(self):
    self.response.out.write(template.render('index.html', None))

我得到的错误:

名称错误: 未定义全局名称"模板"

有趣的是,它适用于 Jinja2 模板。但是,所有HTML代码都是使用Django模板编写的,我认为将它们全部转换太耗时了。这是有效的Jinja2代码(为简单起见,全部在一个代码块中)。

libraries:
- name: jinja2
  version: latest
import jinja2
import os
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class Main(webapp2.RequestHandler):
  def get(self):
    template = jinja_environment.get_template('index.html')
    self.response.out.write(template.render())

您的template未定义;您需要从webapp导入它:

from google.appengine.ext.webapp import template

webapp2向后兼容webapp但您仍然需要使用webapp模板引擎,请参阅使用模板。

相关内容

最新更新