0

I would much appreciate some help on rendering a JSON object on client-side from a Google App Engine entity.

Here is all of my 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 my script tag on the template page, I include

<script>
var jsonobject = JSON.parse({{ postdict['startdate']}});
var jsonobject1 = new Date(jsonobject);
</script>    

Am I on the right track here? I would like to ultimately use the datetime object on the client-side for some Jquery functions.

Any help in pointing me in the right direction would be much appreciated.

JK140
  • 775
  • 2
  • 10
  • 19
  • what language are you writing in on the app engine side? and have you dumped the object on the web browser side? console.log(...) the playload? – saturnism Oct 30 '15 at 22:25
  • Thanks a lot for reply. I'm using python on app engine side. I have not dumped the object on the web browser side. Could you advise me on how to do this? – JK140 Oct 31 '15 at 23:43
  • Does this help? http://stackoverflow.com/q/4830535/584846 – Brent Washburne Nov 02 '15 at 17:43

1 Answers1

1

There are a number of problems with your code. First of all, once you convert post to a dictionary postdict, you can't reference the elements with the dot notation. You have to use a dictionary reference postdict['startdate'] to reference the startdate element.

Next, I use the isoformat() function when storing a date in JSON format:

postdict['startdate'] = postdict['startdate'].isoformat()

This makes it easier to convert to a Javascript Date object:

<script>
var jsonobject = JSON.parse({{ postdict.jsonobject}});
jsonobject[0].y = new Date(jsonobject[0].y);
</script>
Brent Washburne
  • 12,904
  • 4
  • 60
  • 82
  • Thanks so much for the reply. I have the following code now: `postdict = post.to_dict()` `x = postdict['startdate'].isoformat()` `y = [ {'y':x} ]` `jsonobject = json.dumps(y)` `postict['jsonobject'] = jsonobject` And in my script tag for this template, I have: `var jsonobject = JSON.parse( {{ postdict['jsonobject'] }} );` `jsonobject[0].y = new Date(jsonobject[0].y);` However, when I reference the variable jsonobject[0].y in a jQuery function, I get an error in the console that jsonobject is undefined and JSON.parse requires at least 1 parameter. – JK140 Oct 31 '15 at 23:35
  • How are you rendering your template? `template.render(postdict)`? – Brent Washburne Nov 01 '15 at 02:27
  • I use the following functions: `def write(self, *a, **kw): self.response.out.write(*a, **kw)`, `def render_str(self, template, **params): return render_str(template, **params)` and `def render(self, template, **kw): self.write(self.render_str(template, **kw))` – JK140 Nov 01 '15 at 03:11
  • Please update your question with all the code, including the call with `postdict`. – Brent Washburne Nov 01 '15 at 16:10
  • Thanks I've included all the code now. I very much appreciate your help. I'm new to programming and I'm having a hard time understanding how to get the datetime property for one my entities on the client-side so I can use it in jQuery functions. – JK140 Nov 01 '15 at 18:06