About
RSS

Bit Focus


GAE 速成简易博客 - 开张

Posted at 2012-01-19 03:30:27 | Updated at 2024-04-19 06:39:14

前期准备

目前 GAE 官方推荐的 Python 版本是 2.7 (终于脱离了 2.5 的泥潭啊), 实际上 2.6.x 版本也是没问题的 (写个 CMS 这种简易的设备, 应该还用不到太多高端的语言特性).
GAE 当然也是必不可少的. 下载和安装步骤在 Google Code 上都有详细文档. 如果使用 ArchLinux, 还可以通过 AUR 安装. 这里就不废话了.
找个地方, 建立一个目录, 比如叫做 cms-build, 切到这个目录作为工作目录.
下面开始.

数据结构

既然是写博客, 那么最关键的当然要是文章 (post) 了. 先来建立用于定义数据的源文件 model.py
from google.appengine.ext import db

class Post(db.Model):
    title = db.StringProperty(multiline=False)
    content = db.TextProperty()
每篇文章的基本属性有标题 title, 内容 content 和创建时间 date. 其中标题不允许多行; 而内容使用 db.TextProperty 类型而不是 db.StringProperty (根据 GAE 文档, StringProperty 只能存至多 500 字符).

首页

模板

    首先, 将首页给弄出来, 建立目录 templates, 并在这个目录下弄一个 index.html 文件, 内容是
<html>
<head><title>My Blog</title></head>
<body>
{% for post in posts %}
<h1>{{ post.title }}</h1>
<p>{{ post.content }}</p>
{% endfor %}
    在 GAE 中, HTML 模板文件使用的是 django 的模板语法. 上面文件中, {% for ... %}{% endfor %} 是对传入模板的参数 posts 的迭代. {{ post.title }} 则是对 post 对象的 title 的引用, 而 {{ post.content }} 则是引用 content.
    简而言之 {% xxx %} 是模板中的控制语句, 而 {{ xxx }} 则是引用值. 在 django 官方网站可以看到详细文档, 鄙博客之前也对 django 有过简介.

    这个首页是简单了点, 不过呢, 先看看样子.

视图源文件

    光有 HTML 模板还不行, 至少, Python 源代码才是核心. 现在添加一个 Python 源文件 index.py
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import os

from model import Post

class Index(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
        post0 = Post()
        post0.title = 'Hello'
        post0.content = 'This is the first post.'
        post1 = Post()
        post1.title = 'World'
        post1.content = 'This is the second post.'
        self.response.out.write(template.render(path, {
                'posts': [post0, post1],
            }))
    类 Index 继承自 GAE 内置的 google.appengine.ext.webapp.RequestHandler, 从名字可以看出来, 是处理网络请求的单元. 在这个单元的 get 方法中做了下面的事情
    在浏览器中, 通过 URL 直接访问站点使用的是 HTTP Get 方法, 相应的, 如果使用 HTTP Post 方法访问, 在 RequestHandler 子类中需要重写的函数应为 post(self). 这里先不用管那么多, 以后会有例子.

请求路径映射与站点配置文件

    现在干活的源代码都有了, 还差一个引子. 建立文件 main.py
import wsgiref.handlers
from google.appengine.ext import webapp

import index

if __name__ == '__main__':
    application = webapp.WSGIApplication([
        ('/', index.Index),
    ], debug=True)
    wsgiref.handlers.CGIHandler().run(application)
    这一段代码将请求的路径 / 映射到之前编写的 index.Index 类而不是实例, 实例化的过程不需要操心 (因此, 也不要贸然重写初始化函数 __init__, 只需要管好 get, post 这些函数就好).
    最后是站点配置文件 app.yaml
application: my-cms
version: developping
runtime: python
api_version: 1

handlers:
- url: /.*
  script: main.py
    如果要将应用发布到 GAE 上, 配置文件中的 application: my-cmsversion: developping 是需要修改的, application 对应的是在 GAE 上申请的应用名称, 而 version 对应的是应用的版本. 在 GAE 服务器上会根据 version 的不同, 保存应用的多个版本副本, 方便在线测试 (否则, 不小心上传了一个随便一动就崩溃的版本覆盖了原来的版本就悲催了).
    而 handlers 域则配置 URL 规则, 根据 URL 模式来决定由哪一个 Python 源文件 (或者, 以后还会提到, 如何绑定静态文件) 来处理请求. 上面的配置中 - url: /.* 表示对于任意的请求, 其中 .* 是 Python 正则表达式, 表示任意字符序列, 这些请求都会被发送给 main.py 处理.

    现在, 执行下面的命令启动 GAE 本地服务器
$ google_appengine/dev_appserver.py .
其中 google_appengine 是 GAE SDK 的路径, 如果 GAE 是使用 AUR 安装的, 那么 dev_appserver.py 会被加入 path.
    访问 http://localhost:8080/ 即可看到博客系统的雏形了.

下节预告 - 表单, ORM 与读取数据

Post tags:   Tutorial  Web Server  Template  Python  Google AppEngine

Leave a comment:




Creative Commons License Your comment will be licensed under
CC-NC-ND 3.0


. Back to Bit Focus
NijiPress - Copyright (C) Neuron Teckid @ Bit Focus
About this site