尝试将web.py框架中的todolist转移到新浪云中的过程

     一直想尝试用一下新浪云的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-08-02 11:26:57

尝试将web.py框架中的todolist转移到新浪云中的过程的相关文章

解读Python的web.py框架下的application.py模块

  这篇文章主要介绍了Python的web.py框架下的application.py模块,作者深入分析了web.py的源码,需要的朋友可以参考下 本文主要分析的是web.py库的application.py这个模块中的代码.总的来说,这个模块主要实现了WSGI兼容的接口,以便应用程序能够被WSGI应用服务器调用.WSGI是Web Server Gateway Interface的缩写,具体细节可以查看WSGI的WIKI页面 接口的使用 使用web.py自带的HTTP Server 下面这个例子来

使用Python的web.py框架实现类似Django的ORM查询的教程

  这篇文章主要介绍了使用Python的web.py框架实现类似Django的ORM查询的教程,集成的ORM操作数据库向来是Python最强大的功能之一,本文则探讨如何在web.py框架上实现,需要的朋友可以参考下 Django中的对象查询 Django框架自带了ORM,实现了一些比较强大而且方便的查询功能,这些功能和表无关.比如下面这个例子: ? 1 2 3 4 5 6 7 class Question(models.Model): question_text = models.CharFie

安装Python的web.py框架并从hello world开始编程_python

最近有一个小的web项目,想用喜爱都python,但是想到之前接触过都django我感觉一阵不寒而栗,为什么?Django的配置太过复杂,而且小项目不太适合MVC的开发模式,所以我将目光转向了web.py这个小型web框架,并且真正让我动心都是其官方网站上都一句话:"Django lets you write web apps in Django. TurboGears lets you write web apps in TurboGears. Web.py lets you write we

一周中概股前瞻:新浪百度等发布财报

摘要: 斯凯网络2月24日发布第三财季财报 斯凯 网络(Nasdaq: MOBI )将于美国东部时间2月24日美国股市开盘前(北京时间2月24日晚间)发布截至2013年12月31日的2014财年第三季度财报.财报发布后,斯凯 斯凯网络2月24日发布第三财季财报 斯凯网络(Nasdaq: MOBI )将于美国东部时间2月24日美国股市开盘前(北京时间2月24日晚间)发布截至2013年12月31日的2014财年第三季度财报.财报发布后,斯凯管理团队将于美国东部时间2月24日上午8点(北京时间2月24

web.py在SAE中的Session问题解决方法

  这篇文章主要介绍了web.py在SAE中的Session问题解决方法(使用mysql存储),本文直接给出实现代码,代码中包含详细注释,需要的朋友可以参考下 这段时间一直想尝试着在SAE中使用Python,初步选择了Web.py框架做为开发框架,但是可怜SAE上的资料少的可怜,有点问题基本上解决不了,今天解决一个Session在Session的存储问题,在SAE中不能直接用本地文件存储,好像是权限的原因,我现在采用的是保存在mysql中,效果也不错.希望对大家有帮助.直接上代码了. index

简单而直接的Python web 框架:web.py

来源:https://www.oschina.net/question/5189_4306 web.py 是一个Python 的web 框架,它简单而且功能强大.web.py 是公开的,无论用于什么用途都是没有限制的. 先让大家感受一下web.py 的简单而强大: import web urls = ( '/(.*)', 'hello' ) class hello: def GET(self, name): i = web.input(times=1) if not name: name = '

解决web.py在SAE云中的Session使用问题

这段时间一直想尝试着在SAE中使用Python,初步选择了Web.py框架做为开发框架,但是可怜SAE上的资料少的可怜,有点问题基本上解决不了,今天解决一个Session在Session的存储问题,在SAE中不能直接用本地文件存储,好像是权限的原因,我现在采用的是保存在mysql中,效果也不错.希望对大家有帮助.直接上代码了. index.wsgi #!/usr/bin/env python # coding: utf-8 import os import web import sae from

iPad社交杂志Flipboard联合新浪推出中国定制版

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 iPad社交杂志Flipboard联合新浪推出中国定制版 新浪科技讯 12月6日午间消息,iPad社交杂志应用Flipboard今日宣布联合新浪网推出Flipboard中国定制版,包括中国本地化内容.新浪微博共享帖等. Flipboard受全球数百万用户青睐,并被苹果评为"年度iPad应用". Flipboard目前正在为

Python的Flask框架中web表单的教程

  下面为你介绍了Python的Flask框架中web表单的教程,表单是学习各个web框架中的基础知识,需要的朋友可以参考下 概要 在前面章节我们为主页定义了一个简单的模板,部分尚未实现的模块如用户或帖子等使用模拟的对象作为临时占位. 本章我们将看到如何利用web表单填补这些空白. web表单是web应用中最基本的构建要素,我们将通过表单来实现用户发帖和应用登录功能. 完成本章内容你需要基于前面章节完成的微博应用代码,请确认这些代码已安装并能正常运行. 配置 Flask-WTF是WTForms项