現在実装中のブログシステムにコメント機能を実装しました。データ構造としては、記事(Entry)と、1対Nの関係になります。記事(Entry)と、タグ(Tag)の関係がN対Nなので、一通りのデータ構造は実装できたことになります。
投稿者の名前、削除パスワードなどの機能は、正月休み中に実装してしまう予定です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #データモデル class Comment(db.Model): comment = db.TextProperty(default = "") entry = db.ReferenceProperty(Entry, collection_name = 'comments') datetime = db.DateTimeProperty(auto_now_add = True) #投稿処理 class PostCommentHandler(AuthHandler): def post(self, key): comment = Comment() comment.entry = Entry.get(key) comment.comment = self.request.get("comment") comment.put() self.redirect("/entry/%s" % key) #表示部分 if commentDetail:#詳細表示モード self.response.out.write(u'<h2>コメント</h2><div class="comments"><a name="comments">') self.response.out.write('<form action="/postComment/%s" method="post"><input type="hidden" name="phpMyAdmin" value="cfc2644bd9c947213a0141747c2608b0" />' % entry.key()) for comment in entry.comments: self.response.out.write('<h3 class="comment">%s</h3>' % comment.comment) self.response.out.write('<input type="text" name="comment">') self.response.out.write("</form></div>") else:#簡易表示モード self.response.out.write(u'<a href="/entry/%s?phpMyAdmin=cfc2644bd9c947213a0141747c2608b0#comments">コメント(%s)' % (entry.key(), entry.comments.count())) |