Basic Blog,basicblog,main.pyimpor
Basic Blog,basicblog,main.pyimpor
main.py
import webapp2import jinja2import osimport urllibfrom google.appengine.ext import dbtemplate_dir = os.path.join(os.path.dirname(__file__), 'templates')jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)class Handler(webapp2.RequestHandler): def write(self, *a, **kw): self.response.out.write(*a, **kw) def render_str(self, template, **params): t = jinja_env.get_template(template) return t.render(params) def render(self, template, **kw): self.write(self.render_str(template, **kw))class Blog(db.Model): subject = db.StringProperty(required = True) content = db.TextProperty(required = True) created = db.DateProperty(auto_now_add = True)class BlogHandler(Handler): def get(self, resource): if (resource != None): blog_id = urllib.unquote(resource) blogs = db.GqlQuery("SELECT * FROM Blog WHERE __key__ = KEY('Blog', " + blog_id + ")") self.render("blog.html", blogs=blogs) else: blogs = db.GqlQuery("SELECT * FROM Blog ORDER BY created DESC") self.render("blog.html", blogs=blogs)class BlogHandler2(Handler): def get(self): blogs = db.GqlQuery("SELECT * FROM Blog ORDER BY created DESC") self.render("blog.html", blogs=blogs)class NewPostHandler(Handler): def render_newpost(self, subject="", content="", error=""): self.render("newpost.html", subject=subject, content=content, error=error) def get(self): self.render_newpost() def post(self): subject = self.request.get("subject") content = self.request.get("content") if subject and content: b = Blog(subject=subject, content=content) b.put() blog_key = b.key().id() self.redirect("/blog/" + str(blog_key)) else: error = "subject and content, please!" self.render_newpost(subject=subject, content=content, error=error)app = webapp2.WSGIApplication([('/blog/([0-9]+)?', BlogHandler), ('/blog', BlogHandler2), ('/blog/newpost', NewPostHandler)], debug=True)
blog.html
<!DOCTYPE HTML><html> <head> <title>CS 253 Blog</title> </head> <style type="text/css"> body { font-family: sans-serif; width: 800px; margin: 0 auto; padding: 10px; } .error { color: red } label { display: block; font-size: 20px; } input[type=text] { width: 400px; font-size: 20px; padding: 2px; } textarea { width: 400px; height: 200px; font-size: 17px; font-family: monospace; } input[type=submit] { font-size: 24px; } hr { margin: 20px auto; } .blog { margin-top: 20px; } .blog-subject { font-weight: bold; font-size: 20px; } .blog-content { margin: 0; font-size: 17px; } .title { font-weight: bold; font-size: 28px; } </style> <body> <a class="title" href="/blog/">CS 253 Blog</a> {% for blog in blogs %} <div class="blog"> <div class="blog-subject">{{blog.subject}}</div> <div>{{blog.created}}</div> <hr> <pre class="blog-content">{{blog.content}}</pre> </div> {% endfor %} </body></html>
newpost.html
<!DOCTYPE HTML><html> <head> <title>CS 253 Blog</title> </head> <style type="text/css"> body { font-family: sans-serif; width: 800px; margin: 0 auto; padding: 10px; } .error { color: red } label { display: block; font-size: 20px; } input[type=text] { width: 400px; font-size: 20px; padding: 2px; } textarea { width: 400px; height: 200px; font-size: 17px; font-family: monospace; } input[type=submit] { font-size: 24px; } hr { margin: 20px auto; } .blog-subject { font-weight: bold; font-size: 20px; } .blog-content { margin: 0; font-size: 17px; } .title { font-weight: bold; font-size: 28px; } </style> <body> <a class="title" href="/blog/">CS 253 Blog</a> <h1>new post</h1> <form method="post"> <label> <div>subject</div> <input type="text" name="subject" value="{{subject}}"> </label> <br> <label> <div>blog</div> <textarea name="content">{{content}}</textarea> </label> <br> <div class="error">{{error}}</div> <input type="submit"> </form> </body></html>
相关内容
- 使用Gmail发送邮件的Python脚本,gmailpython,[Python]代码
- [娱乐] 画菱形,娱乐画菱形,[Python]代码 w
- User signup (HTML and Python),signuppython,[Python]代码#!
- 基于python的web表单提交方法,pythonweb表单,[Python]代码
- rsync同步管理工具,rsync同步工具,[Python]代码#/
- 有一位农场主的遗嘱:第1个儿子分100美元和剩下财产的
- django 获取用户输入,django获取用户,[Python]代码#u
- 一些两位数加上18,它们的个、十位恰好位置对调了,
- 生成文件的MD5和sha1,生成文件MD5sha1,生成文件的MD5和
- 批量转换html内的&amp;#XXXXX;为中文,且转换文件编
评论关闭