首页 > 解决方案 > 如何在 Google Cloud Platform 的 DataStore 中存储值?

问题描述

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

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

我已将其添加到 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 中,但是当我转到我的 DataStore 时,它​​没有实体,如图链接1所示。我试图获得价值,但似乎没有奏效。

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

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

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

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

标签: pythonhtmlgoogle-app-engine

解决方案


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

  1. 您仍然需要在 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. 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()
    

*注意创建“场合”对象时的“事件”,它应该匹配“事件”选择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,您将能够与来自前端的值进行交互。


推荐阅读