Jason, thanks for the reply. I am quite familiar with the Data Viewer.
I wanted to build something a bit more flexible where I could build a
UI so a layperson (someone that doesn't know GQL) could sort through
all that data.
Jason and Gopal,
I found a solution to store the templates in the db. I created a
simple class which has some simple string construction methods to
build a meta template. I'm going to write a blog post about it
hopefully at some point, until then here's a bit of what I have so
far:
#add this method to any datastore classes you want included (note:
only works on the Model class):
from google.appengine.ext import db
class MyModel(db.Model):
name = db.StringProperty()
location = db.StringProperty()
def get_name(self):
return 'MyModel'
#run this code to construct the template and save it to the datastore:
class Meta_template():
def __init__(self,body):
self.body = body
self.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0
Transitional//EN\" \"DTD/xhtml1-transitional.dtd\">")
def write(self,string):
self.body = str(self.body + str(string))
def tags(self,tag,content):
self.write("<%s>%s</%s>" % (tag,str(content),tag))
def plural(string):
return str(string + 's')
def make_template(model):
templates = Template_file.all()
model_name = model.all()[0].get_name()
templates.filter('name =', model_name)
tf = templates.get()
if tf:
tf.delete()
mt = Meta_template(body="")
mt.tags('h2', model_name)
mt.write("<table border='1'>")
mt.write("<tr>")
for property in model.properties():
mt.tags('td', property)
mt.write("</tr>")
mt.write("{% for ")
mt.write(model_name + " in ")
mt.write(plural(model_name) + " %}")
mt.write("<tr>")
for property in model.properties():
mt.tags('td', '{{ %s.%s }}' % (model_name,property))
mt.write("</tr>")
mt.write("{% endfor %}")
mt.write("</table>")
tf = Template_file(name=model_name, body=mt.body)
tf.put()
make_template(MyModel)
#run this code to render the template:
from django.template import Template, Context
class hello(webapp.RequestHandler):
def get(self):
disp = self.response.out.write
def render(template_name, template_values):
path = os.path.join(os.path.dirname(__file__), '%s' %
template_name)
disp(template.render(path, template_values))
model = MyModel
templates = Template_file.all()
templates.filter('name =', model.all()[0].get_name())
tf = templates.get()
t = Template(tf.body)
template_values = { str(model.all()[0].get_name() + 's') :
model.all
() }
c = Context(template_values)
output = t.render(c) #important!
return disp(output)
Lemme know what you think.
On Apr 14, 9:02 pm, Gopal Vaswani <[email protected]> wrote:
> Looking at the implementation of Django admin module might help. I don't
> know exact details but for each data type (integer, String, list, date etc
> it has associated interface objects that it loads to depict the data in the
> correct controls.
> Vaswani
>
> On Mon, Apr 13, 2009 at 11:04 AM, adelevie <[email protected]> wrote:
>
> > Hi. 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!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---