Try to transfer todolist in the web. py framework to Sina cloud

Source: Internet
Author: User

I have been trying to use the python platform of Sina cloud. This afternoon I found a free option to transfer the official web. py example todolist. I feel pretty good. I will share with you the transfer process.

First, I felt that the information on the Sina cloud platform was too small. In fact, some examples made me dizzy. I will share with you the considerations during the conversion.

1. database connection problems.

You need to change the previous database connection information to the private information of the SAE platform.

 
#! /Usr/bin/ENV Python # Coding: utf-8import webimport Sae. constdb = 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 = trueconfig = web. storage (email = 'oooo @ QQ.com ', site_name = 'Task track', site_desc = '', static ='/static ',) web. template. template. globals ['config'] = configweb. template. template. globals ['render'] = renderprint "settings. py is running"

2. The official start example is too confusing. In particular, I feel useless when referencing templates. Change

Render = web. template. Render ('templates/', cache = false)

3. Others feel better, leaving behind with the sourceProgramFor your reference, I hope to help you.

Index. wsgi

 
#! /Usr/bin/ENV Python # Coding: A utf-8import osimport saefrom config. URL import urlsimport webapp = web. application (URLs, globals ()). wsgifunc () Application = Sae. create_wsgi_app (APP)

Todo. py

#! /Usr/bin/ENV Python # Coding: utf-8import webfrom config import settingsfrom datetime import datetimeprint "controllers is running... "render = settings. renderdb = settings. dbtb = '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 ('title is required ', 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 ('did not find this record ', none) I = web. input () status = I. get ('status', 'yes') If status = 'yes': finished = 1 Elif status = 'no': finished = 0 else: Return render. error ('the request you initiated is not allowed ','/') dB. update (TB, finished = finished, where = 'id = $ id', VA Rs = locals () Raise web. seeother ('/') Class EDIT: def get (self, ID): Todo = get_by_id (ID) if not todo: Return render. error ('the record not found ', none) return render. todo. edit (Todo) def post (self, ID): Todo = get_by_id (ID) if not todo: Return render. error ('did not find this record ', none) I = web. input () Title = I. get ('title', none) if not title: Return render. error ('title is required ', none) dB. update (TB, Title = title, where = 'id = $ id', VAR S = locals () return render. Error ('modified successfully! ','/') Class Delete: def get (self, ID): Todo = get_by_id (ID) if not todo: Return render. error ('the record not found ', none) dB. delete (TB, where = 'id = $ id', vars = locals () return render. error ('deleted successfully! ','/') 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-8pre_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-8import webimport Sae. constdb = 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 = trueconfig = web. storage (email = 'oooo @ QQ.com ', site_name = 'Task track', site_desc = '', static ='/static ',) web. template. template. globals ['config'] = configweb. template. template. globals ['render'] = renderprint "settings. py is running"

Hope to help you

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.