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>

评论关闭