How to use Django 1.2 templates in my Google App Engine project?

Can anyone suggest a detailed resource for incorporating django 1.2 templates in our GAE applications? So far i have found

  • docs describing how to pin django files and add them to our project
  • docs to launch your own django projects in GA
  • docs on including libraries 1.0 and 1.1 in our projects

but so far nothing has been described how to use django 1.2 templates in our projects. In particular, how to formulate secret magic at the top of my python script, which will magically convince GAE to use my zipped django library.

I have this in my python script:

import sys sys.path.insert(0, 'django/django.zip') 

And, like the GAE tutorial, here, as I draw the template:

 template_values = { 'formerror': formerror, 'media': media, 'status': status } path = os.path.join(os.path.dirname(__file__), formtemplate) self.response.out.write(template.render(path, template_values) 

But for GAE, there is not enough part to use Django 1.2 to render templates. What is it?

+4
source share
2 answers

I used this:

 from google.appengine.dist import use_library use_library('django', '1.1') from google.appengine.ext.webapp import template 

In this case, I used version 1.1, but I think it should work the same for 1.2.

+3
source

I had the same problem a while ago - I wanted to use version 1.2 for templates instead of 0.96 (which is provided by GAE). The following code seems to work for me.

 # some standard Google App Engine imports (optional) import wsgiref.handlers from google.appengine.ext import webapp from google.appengine.ext import db # Remove Django modules (0.96) from namespace for k in [k for k in sys.modules if k.startswith('django')]: del sys.modules[k] # Force sys.path to have our own directory first, in case we want to import # from it. This way, when we import Django, the interpreter will first search # for it in our directory. sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) # Must set this env var *before* importing any part of Django # (that required in Django documentation) os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' # New Django imports from django.template.loader import render_to_string from django.conf import settings # Configure dir with templates # our template dir is: /templates TEMPLATE_DIR = os.path.join(os.path.dirname(__file__),'templates') settings.configure(TEMPLATE_DIRS = (TEMPLATE_DIR,'') ) 

However, if you only need templates from Django and other APIs, consider Jinja instead. This is what I plan to do.

0
source

Source: https://habr.com/ru/post/1333737/


All Articles