[英]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.我已经在 HTML 索引文件中添加了这个,如下所示。 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.在我的 python 应用程序中,我为数据存储添加了主题和场合的新类。
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 .现在我想将主题和场合值存储到 DataStore 但是当我将 go 存储到我的 DataStore 时,它没有实体,如图链接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?一旦我提交了所有值(消息、主题、场合),我想在提交后将其全部显示在页面上,但不太确定如何做到这一点?
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).不确定是否为两者(场合和主题)创建 class 是 go 的正确方法(您可能只想在 Greeting 类上添加字符串)。 Anyways, lets focus on Subject for a couple of things you are missing:
无论如何,让我们专注于您缺少的几件事的主题:
You still need to map both classes on the Greeting class:您仍然需要 map 两个类的问候 class:
class Greeting(ndb.Model): class 问候语(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)
Include the assignation on the def post(self) Post method:在def post(self) Post 方法中包含分配:
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. *在创建“事件”object 时注意“事件”,它应该与“事件” select html 标记的“名称”属性匹配。
<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>
<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.使用 1 和 2,您将正确地将值存储在 Datastore 上,您可以在 Console > Datastore > Entities 上查看它。 With 3 and 4 you will be able to interact with the values from the frontend side.
使用 3 和 4,您将能够与来自前端的值进行交互。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.