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))
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.
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!
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.
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.
In order to render a template 'in memory', there are a few things you'll need to do:
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"
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`!
You can instantiate a template from text in Django with just template.Template(my_text)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With