简体   繁体   English

如何从代码而不是 Google App Engine 上的文件呈现 django 模板

[英]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.我正在编写一个 Google App Engine 网络应用程序,它将一些 html 呈现给 Django 模板。 I want to either render the template using either a file or just some json thats very similar to that in file.我想使用文件或仅使用与文件中的非常相似的一些 json 来呈现模板。 Is it possible to use Django to render this to a file that is read in and stored in database?是否可以使用 Django 将其呈现为读入并存储在数据库中的文件? The oldAPI.HTML is just an old version of api.html but with some small changes. oldAPI.HTML 只是 api.html 的旧版本,但有一些小的变化。 Rendering Django to the api-html file works fine.将 Django 渲染到 api-html 文件工作正常。

I understand that you can't store files on GAE, how can i dynamically use Django to render to HTML stored in memory?我知道你不能在 GAE 上存储文件,我如何动态地使用 Django 来呈现存储在内存中的 HTML?

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

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.首先,您需要确保为 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:main.py或(无论您的脚本处理程序是什么)中,您需要添加以下几行:

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 :不要忘记在app.yaml包含django

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 .其次,您需要创建一个Template对象,如Google App Engine 模板文档 中所述 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`!

您可以仅使用template.Template(my_text)从 Django 中的文本实例化模板。

不幸的是,没有(内置)方法可以这样做,但是您可以从函数google.appengine.ext.webapp.template._load_user_django (GAE with Python 2.5)或google.appengine.ext.webapp.template._load_internal_django ( GAE with Python 2.7) 并像 GAE 源代码一样编写您自己的包装器覆盖设置和渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM