首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >按降序显示Google App Engine中的内容

按降序显示Google App Engine中的内容
EN

Stack Overflow用户
提问于 2018-06-06 22:56:45
回答 1查看 66关注 0票数 -2

我尝试根据输入时间以降序显示Google App Engine中的记录。我使用DateTimeProperty在“Datastore”中设置了Timestamp列。

代码语言:javascript
复制
from flask import Flask, render_template, request, redirect
from google.appengine.ext import ndb

app = Flask(__name__)

class MessagePost(ndb.Model):
    Title = ndb.StringProperty(required=True)
    MyPost = ndb.StringProperty(required=True)
    Timestamp = ndb.DateTimeProperty(auto_now_add=True)

@app.route("/Posting_message", methods=["POST"])
def posting_messsage():
    title = request.form["your_title"]
    mypost = request.form["title"]

    post_key = MessagePost(Title=title, MyPost=mypost).put()
    retrieved_post = MessagePost.query().order(-MessagePost.Timestamp)

    return render_template("simple_post.html", retrieved_post=retrieved_post.Title, retrieved_title=retrieved_post.MyPost)

@app.route("/")
def post_page():
    return render_template("simple_post.html")

if __name__ == "__main__":
    app.run(debug=True)
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> Twitt post</title>
    <link rel="stylesheet" type="text/css" href="/static/style.css?v=1.8483317">
</head>
<body>
<div>
    <form name="twitt_form" action={{url_for("posting_messsage")}} method="POST">
        <b style="font-size:124%";>TITLE</b><hr> <br>
    <textarea rows="1" cols="20" name="your_title" id='your_title' value="" required></textarea> <br><br>
        <b style="font-size:124%";>WRITE YOUR POST</b><hr> <br>
        <textarea name="title" id='title' value="" required></textarea> <br><br>
    <br>
    <button type="submit" class="button"><b style="font-size:124%";>POST</b></button>
    </form> <br><br><br><br>
</div
    <div id = "lower_part">
        <b style="font-size:124%";>RECENTPOST</b> <br><hr><br>
        <textarea rows="1" cols="20" name="Recent_post" id="my_post" disabled>{{retrieved_post}} {{retrieved_title}}
        </textarea><br><br>
        <button class="delete_button"><b style="font-size:124%";>Delete</b></button>
    </div>
</body>
</html>

这是datastoredata

代码语言:javascript
复制
`Key    Write Ops    ID    Key Name    MyPost    Timestamp    Title
    aghkZXZ-...    8    4504699138998272        eeee    2018-06-06 09:42:42    how 
    aghkZXZ-...    8    5064350557536256        prem    2018-06-06 09:37:46    hi
    aghkZXZ-...    8    5345825534246912        nandha    2018-06-06 09:34:31    hello
    aghkZXZ-...    8    5627300510957568        back    2018-06-06 09:38:32    welcome
    aghkZXZ-...    8    5630599045840896        hahah    2018-06-06 09:43:40    hahah
    aghkZXZ-...    8    6190250464378880        goodwork 2018-06-06 09:38:08 nanda
    aghkZXZ-...    8    6471725441089536        welcome     2018-06-06 09:37:37 hey
    aghkZXZ-...    8    6753200417800192        hard     2018-06-06 09:38:47 work`

我需要My_PostTitle,这是在datastore内,以降序显示在textarea内部。

EN

回答 1

Stack Overflow用户

发布于 2018-06-07 08:26:21

由于ndb的“最终一致性”,实体可能不会立即可用。您的代码要求它立即可用。既然已经有了实体属性,为什么还要花费资源进行查询呢?一旦put()成功,就会设置post_key。因此,您可以对此进行测试,或者只使用已有的属性。在这里查询是一种浪费。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50723498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档