繁体   English   中英

如何在 Google Cloud Platform 的 DataStore 中存储值?

[英]How do I store values in DataStore in Google Cloud Platform?

所以我目前正在使用谷歌留言簿示例应用程序,对此完全陌生。

我想要做的是创建一个文本框,让用户输入一个主题,以及一个下拉菜单,其中包含一个场合列表以及一条消息来登录留言簿。

我已经在 HTML 索引文件中添加了这个,如下所示。 这工作正常并显示页面上的内容。

<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>

在我的 python 应用程序中,我为数据存储添加了主题和场合的新类。

class Subject(ndb.Model):

subject = ndb.StringProperty(indexed=False)

class Occasion(ndb.Model):

occasion = ndb.StringProperty(indexed=False)

现在我想将主题和场合值存储到 DataStore 但是当我将 go 存储到我的 DataStore 时,它没有实体,如图链接1所示。 我试图获得价值,但似乎没有奏效。

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

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

一旦我提交了所有值(消息、主题、场合),我想在提交后将其全部显示在页面上,但不太确定如何做到这一点?

这是我的页面到目前为止的样子 - 2

不确定是否为两者(场合和主题)创建 class 是 go 的正确方法(您可能只想在 Greeting 类上添加字符串)。 无论如何,让我们专注于您缺少的几件事的主题:

  1. 您仍然需要 map 两个类的问候 class:

    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)
  2. 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()

*在创建“事件”object 时注意“事件”,它应该与“事件” select html 标记的“名称”属性匹配。

  1. HTML 标签应该在<form...>里面并且应该是这样的:

 <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. 使用 jinja2 语法在 html 上打印值:

 <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 %}

使用 1 和 2,您将正确地将值存储在 Datastore 上,您可以在 Console > Datastore > Entities 上查看它。 使用 3 和 4,您将能够与来自前端的值进行交互。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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