【问题标题】:How do I store values in DataStore in Google Cloud Platform?如何在 Google Cloud Platform 的 DataStore 中存储值?
【发布时间】:2020-10-03 19:25:01
【问题描述】:

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

我想做的是创建一个允许用户输入主题的文本框,以及一个包含场合列表的下拉菜单以及要在留言簿上签名的消息。

我已将其添加到 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

【问题讨论】:

    标签: python html google-app-engine


    【解决方案1】:

    不确定是否为两者(场合和主题)创建一个类是正确的方法(您可能只想在 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) 发布方法:

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

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

    1. 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>
    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,您将能够与来自前端的值进行交互。

    【讨论】:

    • 我用与上述步骤相同的留言簿教程对其进行了测试,一切顺利。在客户端抛出 500。您可以转到 Stackdriver logging > Filter by App Engine 找到错误详细信息,您能否分享日志中引发的错误的更多详细信息?
    • 我编辑了回复以显示更多详细信息,如果 500 仍然存在,请在 App Engine Stackdriver 日志中分享错误详细信息
    猜你喜欢
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 2021-04-21
    • 2016-04-08
    • 1970-01-01
    • 2016-10-08
    相关资源
    最近更新 更多