Google App Engine--Dynamically created templates

2019-08-20 06:45发布

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!

3条回答
戒情不戒烟
2楼-- · 2019-08-20 07:15

I saw this open source project a while back: http://code.google.com/p/gae-django-dbtemplates/

Using a template to generate a template should be fine. Just render the template to a string. Here some code i use so i can stick some xml into memecache

path = os.path.join(os.path.dirname(__file__), 'line_chart.xml')
xml = template.render(path, template_values)

You can easily do something very similar and stick the result in the datastore.

查看更多
别忘想泡老子
3楼-- · 2019-08-20 07:25

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
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-08-20 07:29

Other option, that in my opinion simplifies writing apps for GAE a lot, is using user other templating language, like Mako, that allows you to embed Python code in the template, thus no fiddling required. You would pass model data to the template (as simple as template.render(template_file, model=model), and the template would look something like this:

<table border='1'>
  <tr>
      % for attribute in model:
        <td>${attribute}</td>
      % endfor
  </tr>
  <tr>
      % for attribute in model:
        <td>${model.attribute}</td>
      % endfor
  </tr>
</table>

I followed this googled blog entry to get Mako in my app - it was quite simple and works like a charm.

查看更多
登录 后发表回答