0

I am in the process of migrating some GAE apps from Python 2.5 to 2.7. It seems much more difficult to import Django templates (any version) into this version of Python. I followed Google's instructions to the T and scoured the web for help, but ultimately failed. 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:

libraries:
- name: django
  version: "1.2"

In 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

Interestingly, it worked with Jinja2 templates. However, all HTML code was written using Django templates and I think it would be too time consuming to convert them all. Here's the Jinja2 code that worked (all in one code block for simplicity).

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())
4

1 回答 1

2

Your template is undefined; you'll need to import it from 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.

于 2012-11-29T09:54:08.477 回答