简体   繁体   中英

How do I store values in DataStore in Google Cloud Platform?

So im currently using the Google Guestbook Sample App and completely new to this.

What I want to do is create a textbox that lets the user put in a subject, and a drop down menu that has a list of occasions as well as a message to sign to the guestbook.

Ive added this in the HTML index file as shown below. This works fine and shows the content on the page.

<div class="subject-area">
    <label for="subject">Subject:</label>
    <textarea id="subject" name="subject" rows="1"></textarea>
    </div>

    <div class="occasion-area">

    <label for="occasions">Choose an Occasion:</label>
        <select id="events">
        <option value="Christmas">Christmas</option>
        <option value="New Year">New Year</option>
        <option value="Easter">Easter</option>
        <option value="Australia Day">Australia day</option>
        </select>
    </div>

In my python application I've added new Classes for Subject and Occasion for the datastore.

class Subject(ndb.Model):

subject = ndb.StringProperty(indexed=False)

class Occasion(ndb.Model):

occasion = ndb.StringProperty(indexed=False)

Now I want to store the subject and occasion value into DataStore but when I go to my DataStore it no entity as show in image link 1 . Ive tried to get the values but didn't seem to work.

greeting.subject = self.request.get('subject')
    greeting.put()

    greeting.occasion = self.request.get('occasion')
    greeting.put()

Once Ive submitted all of the values(message, subject, occasion) I want to display it all on the page after submitting but not quite sure on how to do that yet?

Heres what my page looks like so far - 2

Not sure if creating a class for both (ocassions and subject) is the right way to go (you might just want to add strings on Greeting class). Anyways, lets focus on Subject for a couple of things you are missing:

  1. You still need to map both classes on the Greeting class:

    class Greeting(ndb.Model):

     author = ndb.StructuredProperty(Author) content = ndb.StringProperty(indexed=False) date = ndb.DateTimeProperty(auto_now_add=True) subject = ndb.StructuredProperty(Subject) occassion = ndb.StructuredProperty(Occasion)
  2. Include the assignation on the def post(self) Post method:

     greeting.content = self.request.get('content') greeting.subject = Subject( subject=self.request.get('subject')) greeting.occassion = Occasion( occasion=self.request.get('event')) greeting.put()

*Notice the 'event' when creating the "occasion" object, it should match the "name" attribute for the "events" select html tag.

  1. HTML tags should be inside the <form...> and should look like this:

 <form action="/sign?guestbook_name={{ guestbook_name }}" method="post"> <div class="subject-area"> <label for="subject">Subject:</label> <textarea id="subject" name="subject" rows="1"></textarea> </div> <div class="occasion-area"> <label for="occasions">Choose an Occasion:</label> <select id="events" name="event"> <option value="Christmas">Christmas</option> <option value="New Year">New Year</option> <option value="Easter">Easter</option> <option value="Australia Day">Australia day</option> </select> </div> <div><textarea name="content" class="input-block-level" rows="3"></textarea></div> <div><input type="submit" class="btn btn-large btn-primary" value="Sign Guestbook"></div> </form>

  1. Print the value on the html with the jinja2 syntax:

 <div class="container"> <.-- [START greetings] --> {% for greeting in greetings %} <div class="row"> {% if greeting.author %} <b>{{ greeting.author.email }} {% if user and user.user_id() == greeting.author:identity %} (You) {% endif %} </b> wrote: {% else %} An anonymous person wrote. {% endif %} <blockquote>{{ greeting.subject.subject }}</blockquote> <blockquote>{{ greeting.occasion }}</blockquote> <blockquote>{{ greeting.content }}</blockquote> </div> {% endfor %}

With 1 and 2 you will correctly store the values on Datastore, you can check it out on Console > Datastore > Entities. With 3 and 4 you will be able to interact with the values from the frontend side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM