0

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>
user123429
  • 187
  • 1
  • 8
  • Have a look at https://stackoverflow.com/questions/7322682/best-way-to-store-json-in-an-html-attribute and https://stackoverflow.com/questions/39193510/how-to-insert-arbitrary-json-in-htmls-script-tag – snakecharmerb Oct 08 '17 at 05:35

1 Answers1

0

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.

user123429
  • 187
  • 1
  • 8