python - How to render JSON on client-side from Google App Engine Entity -
i appreciate on rendering json object on client-side google app engine entity.
here of relevant code:
def render_str(template, **params): t = jinja_env.get_template(template) return t.render(params) class bloghandler(webapp2.requesthandler): def write(self, *a, **kw): self.response.out.write(*a, **kw) def render_str(self, template, **params): params['user'] = self.user return render_str(template, **params) def render(self, template, **kw): self.write(self.render_str(template, **kw)) class post(ndb.model): subject = ndb.textproperty(required = true) created = ndb.datetimeproperty(auto_now_add = true) startdate = ndb.datetimeproperty() enddate = ndb.datetimeproperty() class postpage(bloghandler): def get(self, post_id): key = ndb.key('post', int(post_id), parent=blog_key()) post = key.get() postdict = post.to_dict() postdict['startdate'] = postdict['startdate'].isoformat() self.render("permalink.html", postdict = postdict)
in script tag on template page, include
<script> var jsonobject = json.parse({{ postdict['startdate']}}); var jsonobject1 = new date(jsonobject); </script>
am on right track here? use datetime object on client-side jquery functions.
any in pointing me in right direction appreciated.
there number of problems code. first of all, once convert post
dictionary postdict
, can't reference elements dot notation. have use dictionary reference postdict['startdate']
reference startdate
element.
next, use isoformat()
function when storing date in json format:
postdict['startdate'] = postdict['startdate'].isoformat()
this makes easier convert javascript date object:
<script> var jsonobject = json.parse({{ postdict.jsonobject}}); jsonobject[0].y = new date(jsonobject[0].y); </script>
Comments
Post a Comment