Google App Engine - 动态创建的模板 [英] Google App Engine--Dynamically created templates

查看:219
本文介绍了Google App Engine - 动态创建的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建我的应用程序的一个简单的CRUD管理部分。基本上,对于给定的模型,我想通过模型循环遍历模型的属性到一个简单的表(一旦我这样做,我实际上可以实现CRUD部分)。实现此目的的一种可行方法是动态生成具有该模型所需的所有模板标签的模板。



伪代码:



pre> def generate_tamplate(model):
template.write(< table border ='1'>)
template.write( < tr>)
for model中的属性
template.write(< td>%s< / td>%属性)
template.write(< / tr $
template.write(< tr>)
为模型中的属性:
template.write(< td> {{%s。%s}} < / td>%model.attribute)
template.write(< / tr>)
template.write(< / table>)

生成正确的文本不应该是困难的。我可以跟随我的伪代码模型,并做它Python。两件事我想知道:
1)我可以使用Django的模板语言吗?也就是说,使用模板生成一个模板
2)一旦生成文本,我该怎么写到webapp的模板加载器可以访问的文件?



我记得一会儿,看到有关从数据库加载模板的内容。是否可以使用GAE?



谢谢!

解决方案

而不是使用 template.writes ,您可以生成下一个模板 - 因为 template.render(...)只是返回文字。然后,您可以存储返回的文本并将其放入DataStore中,然后再检索,然后调用 .render(Context(...)),以返回html想生成。



您不能将生成的模板写入文件 - 因为AppEngine应用程序没有对文件系统的写入访问权限,只能读取访问权限。



如果您更改' generate_tamplate '函数以使用模板,则伪代码可能如下所示: p>

  from google.appengine.ext.webapp import template 

def generate_tamplate(model):
t = template.render(path_to_template1.html,Context({'model':model}))
DataStoreTemplate(template = t,name = model.name).put()

' '
def generate_page(model)
t = DataStoreTemplate.all()。filter(name =,model.name).get ().template
htmlresult = t.render(Context({'model':model}))
return htmlresult


I'm trying to build an a simple CRUD admin section of my application. Basically, for a given Model, I want to have a template loop through the model's attributes into a simple table (once I do this, I can actually implement the CRUD part). A possible way to accomplish this is to dynamically generate a template with all the necessary template tags specific to that model.

Pseudocode:

def generate_tamplate(model):
     template.write("<table border='1'>")
     template.write("<tr>")
     for attribute in model:
          template.write("<td>%s</td>" % attribute)
     template.write("</tr>")
     template.write("<tr>")
     for attribute in model:
          template.write("<td>{{ %s.%s }}</td>" % model.attribute)
     template.write("</tr>")
     template.write("</table>")

Generating the proper text should not be difficult. I can follow my pseudocode model and do it Python. Two things im wondering: 1) Can I do this instead using Django's templating language? that is, use a template to generate a template 2) Once I generate the text, how can I wrote that to a file that webapp's template loader can access?

I remember a while back seeing something about loading template from the database. Is this possible with GAE?

THANKS!

解决方案

Yes, instead of doing template.writes, you can generate the next template - since template.render(...) just returns text. You can then store the text returned and put it into the DataStore, then retrieve it later and call .render(Context(...)) on it to return the html you want to generate.

You cannot write the generated template to a file - as AppEngine applications do not have write access to the filesystem, only read access.

If you change your 'generate_tamplate' function to use a template, the pseudocode could look like this:

from google.appengine.ext.webapp import template

def generate_tamplate(model):
    t = template.render(path_to_template1.html, Context({'model':model}))
    DataStoreTemplate(template=t, name=model.name).put()

''' Later, when you want to generate your page for that model '''
def generate_page(model):
    t = DataStoreTemplate.all().filter("name =",model.name).get().template
    htmlresult = t.render(Context({'model':model}))
    return htmlresult

这篇关于Google App Engine - 动态创建的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆