我尝试根据输入时间以降序显示Google App Engine中的记录。我使用DateTimeProperty
在“Datastore”中设置了Timestamp
列。
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)
<!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>
这是datastore
的data
`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_Post
和Title
,这是在datastore
内,以降序显示在textarea
内部。
发布于 2018-06-07 08:26:21
由于ndb的“最终一致性”,实体可能不会立即可用。您的代码要求它立即可用。既然已经有了实体属性,为什么还要花费资源进行查询呢?一旦put()
成功,就会设置post_key
。因此,您可以对此进行测试,或者只使用已有的属性。在这里查询是一种浪费。
https://stackoverflow.com/questions/50723498
复制相似问题