一直想尝试用一下新浪云的Python平台,今天下午找个一个空闲将web.py的官方例子todolist转移了一下。感觉还不错,将转移的过程与大家分享一下。
首先感觉新浪云平台的资料太不细了,其实有些例子反而让我有点头晕。我将转换中的注意事项与大家分享一下。
1、数据库连接问题。
需要将以前的数据库连接信息改一下,转换成SAE平台的专有信息
#!/usr/bin/env python # coding: utf-8 import web import sae.const db = web.database(dbn='mysql', user=sae.const.MYSQL_USER, pw=sae.const.MYSQL_PASS, host=sae.const.MYSQL_HOST, port=3307, db=sae.const.MYSQL_DB) render = web.template.render('templates/', cache=False) web.config.debug = True config = web.storage( email='oooo@qq.com', site_name = '任务跟踪', site_desc = '', static = '/static', ) web.template.Template.globals['config'] = config web.template.Template.globals['render'] = render print "settings.py is running"
2、官方给的起始例子太迷惑人了。特别是对模板的引用,我感觉没有啥用。我直接改成
render = web.template.render('templates/', cache=False)
3、其它的感觉还好,留下截图与源程序给大家参考,希望对大家有帮助。
index.wsgi
#!/usr/bin/env python # coding: utf-8 import os import sae from config.url import urls import web app = web.application(urls, globals()).wsgifunc() application = sae.create_wsgi_app(app)
todo.py
#!/usr/bin/env python # coding: utf-8 import web from config import settings from datetime import datetime print "controllers is running..." render = settings.render db = settings.db tb = 'todo' def get_by_id(id): s = db.select(tb, where='id=$id', vars=locals()) if not s: return False return s[0] class New: def POST(self): i = web.input() title = i.get('title', None) if not title: return render.error('标题是必须的', None) db.insert(tb, title=title, post_date=datetime.now()) raise web.seeother('/') class Finish: def GET(self, id): todo = get_by_id(id) if not todo: return render.error('没找到这条记录', None) i = web.input() status = i.get('status', 'yes') if status == 'yes': finished = 1 elif status == 'no': finished = 0 else: return render.error('您发起了一个不允许的请求', '/') db.update(tb, finished=finished, where='id=$id', vars=locals()) raise web.seeother('/') class Edit: def GET(self, id): todo = get_by_id(id) if not todo: return render.error('没找到这条记录', None) return render.todo.edit(todo) def POST(self, id): todo = get_by_id(id) if not todo: return render.error('没找到这条记录', None) i = web.input() title = i.get('title', None) if not title: return render.error('标题是必须的', None) db.update(tb, title=title, where='id=$id', vars=locals()) return render.error('修改成功!', '/') class Delete: def GET(self, id): todo = get_by_id(id) if not todo: return render.error('没找到这条记录', None) db.delete(tb, where='id=$id', vars=locals()) return render.error('删除成功!', '/') class Index: def GET(self): todos = db.select(tb, order='finished asc, id asc') return render.index(todos)
url.py
#!/usr/bin/env python # coding: utf-8 pre_fix = 'controllers.' urls = ( '/', pre_fix + 'todo.Index', '/todo/new', pre_fix + 'todo.New', '/todo/(\d+)', pre_fix + 'todo.View', '/todo/(\d+)/edit', pre_fix + 'todo.Edit', '/todo/(\d+)/delete', pre_fix + 'todo.Delete', '/todo/(\d+)/finish', pre_fix + 'todo.Finish', '/todo/test','mycontrollers.todo.mytest' )
setttings.py
#!/usr/bin/env python # coding: utf-8 import web import sae.const db = web.database(dbn='mysql', user=sae.const.MYSQL_USER, pw=sae.const.MYSQL_PASS, host=sae.const.MYSQL_HOST, port=3307, db=sae.const.MYSQL_DB) render = web.template.render('templates/', cache=False) web.config.debug = True config = web.storage( email='oooo@qq.com', site_name = '任务跟踪', site_desc = '', static = '/static', ) web.template.Template.globals['config'] = config web.template.Template.globals['render'] = render print "settings.py is running"
希望对大家有帮助
时间: 2024-10-03 03:16:06