Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to render django template from code instead of file on Google App Engine

I am writing a Google App Engine webapp that renders some html to a Django template. I want to either render the template using either a file or just some json thats very similar to that in file. Is it possible to use Django to render this to a file that is read in and stored in database? The oldAPI.HTML is just an old version of api.html but with some small changes. Rendering Django to the api-html file works fine.

I understand that you can't store files on GAE, how can i dynamically use Django to render to HTML stored in memory?

path = ""
oldAPI = APIVersion().get_by_key_name(version)
if oldAPI is None:
    path = os.path.join(os.path.dirname(__file__), "api.html")
template_values = {
            'responseDict': responseDict,
            }
        if path:
            self.response.out.write(template.render(path, template_values))
        else:
            self.response.out.write(template.render(oldAPI.html,template_values))
like image 572
bogen Avatar asked Mar 15 '13 12:03

bogen


People also ask

How do I use a downloaded Django template?

To configure the Django template system, go to the settings.py file and update the DIRS to the path of the templates folder. Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps.

Which option does Django templates accept?

DjangoTemplates engines accept the following OPTIONS : 'autoescape' : a boolean that controls whether HTML autoescaping is enabled. It defaults to True . Only set it to False if you're rendering non-HTML templates!

Why template does not exist Django?

Django TemplateDoesNotExist error means simply that the framework can't find the template file. To use the template-loading API, you'll need to tell the framework where you store your templates. The place to do this is in your settings file ( settings.py ) by TEMPLATE_DIRS setting.

Can we write Python code in Django template?

You cannot use python code in django template. This is by design, Django's idea of template is to isolate the presentation logic from the programming code.


2 Answers

In order to render a template 'in memory', there are a few things you'll need to do:

App Engine Setup

First of all, you'll need to ensure that everything is set up correctly for Django. There's a lot of information on the Third-party libraries page, but I'll include it here for your benefit.

In main.py, or (whatever your script handler is), you'll need to add the following lines:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2') # Change to a different version as you like

Don't forget to include django in your app.yaml:

libraries:
    - name: django
      version: "1.2"

Code Setup

Second of all, you'll need to create a Template object, as denoted in the Google App Engine template documentation. For example:

from google.appengine.ext.webapp import template

# Your code...
template_string = "Hello World"
my_template = template.Template(template_string)

# `context` is optional, but will be useful!
# `context` is what will contain any variables, etc. you use in the template
rendered_output = template.render(context)

# Now, do what you like with `rendered_output`!
like image 153
NT3RP Avatar answered Sep 19 '22 08:09

NT3RP


You can instantiate a template from text in Django with just template.Template(my_text).

like image 21
Daniel Roseman Avatar answered Sep 20 '22 08:09

Daniel Roseman