Search code examples
javascriptpythongoogle-app-enginejinja2webapp2

Storing a Javascript Object in GAE using Jinja2


What is the easiest way to save a javascript object to GAE datastore (Python) through Jinja2? I tried looking in the Jinja2 documentation, but I couldn't find any relevant info. See below for my code and my commentary. I included an example below that shows where my confusion is. I want to store the javascript variable "Article" as JSON in GAE. Any help would be GREATLY appreciated. Thanks!

Python

class Post(ndb.Model):
    subject = ndb.StringProperty(required = True)
    content = ndb.TextProperty(required = True)
    quilljs = ndb.StringProperty(required = True)

class NewPost(PostHandler)
    def post(self):
         subject = self.request.get('subject')
         content = self.request.get('content')
         quilljs = self.request.get('quilljs')
        if subject and content:
             p = Post(parent = blog_key(), subject = subject, content = content, quilljs = quilljs)
            p.put()

HTML

 <form method="post">
      <label>
        <div>subject</div>
        <input type="text" name="subject" value="{{subject}}">
      </label>

      <label>
        <div>blog</div>
        <textarea name="content">{{content}}</textarea>
      </label>
      <div id="quilljs">
          script type="text/javascript">
          var Article = "{{ json_data }}";
          #THIS IS THE PART I HAVE NO IDEA HOW TO DO
          </script>
      </div>
      <input type="submit"></input>
</form>

Solution

  • I ended up using AJAX to pass the JS object over as a JSON object (as well as the other HTML values). Although not the most eloquent solution, it works.