简体   繁体   English

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

[英]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?一旦我提交了所有值(消息、主题、场合),我想在提交后将其全部显示在页面上,但不太确定如何做到这一点?

Heres what my page looks like so far - 2这是我的页面到目前为止的样子 - 2

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:无论如何,让我们专注于您缺少的几件事的主题:

  1. 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)
  2. 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 标记的“名称”属性匹配。

  1. HTML tags should be inside the <form...> and should look like this: 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. Print the value on the html with the jinja2 syntax:使用 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 %}

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.

相关问题 Google Cloud Platform:我如何获得使用Python将对象放入Google Cloud Store的签名URL - Google Cloud Platform: How can I get a signed URL for putting an object to Google Cloud Store with Python 谷歌云平台 - 数据存储 - Python ndb API - Google Cloud Platform - Datastore - Python ndb API 如何将辅助python文件上传到Google云平台? - How do I upload a secondary python file to google cloud platform? 如何将文件存储在googleappengine数据存储区中 - How do I store files in googleappengine datastore 如何仅从 Google Cloud Datastore 实体中提取密钥 - How do I extract only the key from a Google Cloud Datastore entity 如何在 python 程序中定义我的 Google Cloud Platform 服务帐户的密钥? - How do I define my Google Cloud Platform service account's key in a python program? 如何将本地Google App Engine Python数据存储区复制到本地Google App Engine Java数据存储区? - How do I copy local Google App Engine Python datastore to local Google App Engine Java datastore? 如何使用Google Cloud Datastore统计信息 - How to use Google Cloud Datastore Statistics Google Cloud Platform - AI Platform:为什么调用 API 时会收到不同的响应正文? - Google Cloud Platform - AI Platform: why do I get different response body when calling API? 如何在 Google 云平台 gcp 上托管 python 脚本? - How can I host a python script on Google cloud platform gcp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM