我尝试从数据库中获取数据并在网页中显示数据,并将注释保存回数据库。
我使用Python构建服务器和Google App Engine数据库作为我的数据库,因为我找不到任何有用的API可以为我提供有用的日期。
对于前端,我尝试使用JavaScript来实现网页显示和处理数据库。
但是,我不知道如何解决连接问题,这是: 1,获取日期后,如何将gql查询转换为JSON,并发送到前端。 2,用户输入后,如何保存回google app engin数据库。
非常感谢,如果有人愿意帮助我。
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name=viewport content="width=device-width, initial-scale=1">
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" src="../script.js"></script>
</head>
<body>
<ul id="pagerender">
</ul>
</body>
</html>
服务器代码:
import webapp2, jinja2, os, re, json
from google.appengine.ext import db
class User(db.Model):
username = db.StringProperty(required = True)
pw_hash = db.StringProperty(required = True)
email = db.StringProperty()
class Post(db.Model):
img = db.LinkProperty(required = True)
body = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
author = db.ReferenceProperty(User, required = True)
class Comment(db.Model):
postid = db.StringProperty(required = True)
cmt = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
class MainPageHandler(webapp2.RequestHandler):
def render_template(self, template_name, **context):
if not self.user:
me = "Welcome Visit"
else:
me= self.user.username
t = jinja_env.get_template(template_name)
response = t.render(me=me, **context)
return response
def get(self, username=""):
"""Fetch posts"""
posts = Post.all().order('-created')
self.response.out.write(json.dumps(posts))
response = self.render_template('mainpage.html', posts=posts, username=username)
def post(self,id):
post = Post.get_by_id(int(id))
if post:
submitted_comment = self.request.get("commentname")
# commentauthor = self.request.get("commentauthor")
comment = Comment(
postid = id,
cmt= submitted_comment
)
comment.put()
comments = db.GqlQuery("SELECT * FROM Comment WHERE postid = '%s'"% id)
# t = jinja_env.get_template("post.html")
response = self.render_template("post.html", post=post,comments=comments)
self.response.out.write(response)
app = webapp2.WSGIApplication([
('/.*', MainpageHandler)], debug=True)
JS代码:
function render(){
// window.location.reload();
$.ajax("/",{
type:"GET",
data:{
fmt: 'json'
},
success: function(response){
response.results.forEach(function(post)){
var post_title = $("<h3 />").text(post.title);
var post_img = $("<img/>").attr("src", post.img);
var post_body = $("<P/>").text(post.body);
var post_user = $("<p/>").attr("src", /sparkle/post.key().id()).text(post.author.username);
var post_comment = $("<textarea/>").attr("id", commentconcept);
var post_buttom = $("<button/>").attr("id", comment+post.key().id()).click(function(){
postid = post.key().id());
cmt = $("#commentconcept").val();
comment = Comment(
postid = postid,
cmt= cmt
)
comment.put()
});
var item_view = $("<li></li>").append(post_title),
.append(post_img),
.append(post_body),
.append(post_user),
.append(post_comment),
append(post_buttom);
$("#pagerender").append(item_view);
}
}
});
}
$(document).ready(function() {
render();
});