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