简体   繁体   English

Google App Engine中的Django - Python 2.7

[英]Django in Google App Engine - Python 2.7

I am in the process of migrating some GAE apps from Python 2.5 to 2.7. 我正在将一些GAE应用程序从Python 2.5迁移到2.7。 It seems much more difficult to import Django templates (any version) into this version of Python. 将Django模板(任何版本)导入到此版本的Python中似乎要困难得多。 I followed Google's instructions to the T and scoured the web for help, but ultimately failed. 我按照谷歌的指示去了T并搜索网页寻求帮助,但最终失败了。 So here is what I tried, and I was wondering if any of you guys would be able to help me! 所以这就是我尝试过的,我想知道你们中的任何人是否能够帮助我! Thanks in advance. 提前致谢。

In app.yaml: 在app.yaml中:

libraries:
- name: django
  version: "1.2"

In main.yaml: 在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()

The main class: 主要课程:

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

The Error I get: 我得到的错误:

NameError: global name 'template' is not defined NameError:未定义全局名称“模板”

Interestingly, it worked with Jinja2 templates. 有趣的是,它适用于Jinja2模板。 However, all HTML code was written using Django templates and I think it would be too time consuming to convert them all. 但是,所有HTML代码都是使用Django模板编写的,我认为将它们全部转换起来太费时间了。 Here's the Jinja2 code that worked (all in one code block for simplicity). 这是有效的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())

Your template is undefined; 您的template未定义; you'll need to import it from webapp : 你需要从webapp导入它:

from google.appengine.ext.webapp import template

webapp2 is backwards compatible with webapp but you'll need to use the template engine from webapp still, see Using templates . webapp2向后兼容webapp但您仍需要使用webapp的模板引擎,请参阅使用模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM