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())
15
  • You are missing a definition for the variable template. This has nothing to do with the template engine. Commented Nov 29, 2012 at 8:47
  • Thanks for your input. In the past I could just use: from google.appengine.ext.webapp import template. But that doesn't seem to work now.
    – mrmo123
    Commented Nov 29, 2012 at 8:51
  • webapp is a different framework from Django. Did you follow the Django tutorials at all on how to define templates and use them? Commented Nov 29, 2012 at 8:52
  • 3
    In fact, you are not using Django in your main app, you are still using webapp. Adding Django to your yaml file will not magically make webapp know about Django templates. Commented Nov 29, 2012 at 8:54
  • ohh woops, I see now :) So in the past I guess I was using webapp for my main app and Django templates for HTML. Do you know if it's possible to do the same in GAE python 2.7? I guess that's what I'm attempting to do.
    – mrmo123
    Commented Nov 29, 2012 at 8:58

1 Answer 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.