Web. py: 10 minutes to create simple blog implementation code, and web. py: 10 minutes
1. Introduction to web. py
Web. py is a lightweight Python web development framework. It is simple, efficient, and has low learning costs. It is especially suitable as a beginner framework for python web development. Site: http://webpy.org/
Ii. web. py Installation
1. Download: http://webpy.org/static/web.py-0.33.tar.gz
2. Unzip and enter the web. py-0.33 directory, install: python setup. py install
3. Create a simple blog
1. Directory Description: main directory blog/, template directory blog/templates
2. Create the table "entries" in the Database "test"
CREATE TABLE entries ( id INT AUTO_INCREMENT, title TEXT, content TEXT, posted_on DATETIME, primary key (id) );
3. Create blog. py, blog/blog. py in the main directory
# Loading framework import web # loading database operation model (created later) import model # URL ing urls = ('/', 'index', '/view/(/d +) ', 'view','/new', 'new', '/delete/(/d +)', 'delete', '/edit/(/d +) ', 'edit','/login', 'login', '/logout', 'logout',) app = web. application (urls, globals () # template public variable t_globals = {'datestr': web. datestr, 'cookies': web. cookies,} # specify the template directory and set the public template render = web. template. render ('templates', base = 'base', globals = t_globals) # create a login form login = web. form. form (web. form. textbox ('username'), web. form. password ('Password'), web. form. button ('login') # homepage class Index: def GET (self): login_form = login () posts = model. get_posts () return render. index (posts, login_form) def POST (self): login_form = login () if login_form.validates (): if login_form.d.username = 'admin'/and login_form.d.password = 'admin': web. setcookie ('username', login_form.d.username) raise web. seeother ('/') # View the article class View: def GET (self, id): post = model. get_post (int (id) return render. view (post) # create an article class New: form = web. form. form (web. form. textbox ('title', web. form. notnull, size = 30, description = 'Post title: '), web. form. textarea ('content', web. form. notnull, rows = 30, cols = 80, description = 'Post content: '), web. form. button ('Post entry '),) def GET (self): form = self. form () return render. new (form) def POST (self): form = self. form () if not form. validates (): return render. new (form) model. new_post (form. d. title, form. d. content) raise web. seeother ('/') # Delete the article class Delete: def POST (self, id): model. del_post (int (id) raise web. seeother ('/') # Edit the article class Edit: def GET (self, id): post = model. get_post (int (id) form = New. form () form. fill (post) return render. edit (post, form) def POST (self, id): form = New. form () post = model. get_post (int (id) if not form. validates (): return render. edit (post, form) model. update_post (int (id), form. d. title, form. d. content) raise web. seeother ('/') # log out class Logout: def GET (self): web. setcookie ('username', '', expires =-1) raise web. seeother ('/') # define def notfound (): return web. notfound ("Sorry, the page you were looking for was not found. ") app. notfound = notfound # Run if _ name _ = '_ main _': app. run ()
4. Create model. py, blog/model. py in the main directory
Import webimport datetime # database connection db = web. database (dbn = '<a href = "http://lib.csdn.net/base/14" class = 'replace _ word' title = "MySQL knowledge base" target =' _ blank 'style = 'color: # df3434; font-weight: bold; '> MySQL </a>', db = 'test', user = 'root', pw = '000000 ') # retrieve all articles def get_posts (): return db. select ('entries', order = 'id desc') # obtain the article content def get_post (id): try: return db. select ('entries', where = 'id = $ id', vars = locals () [0] couldn t IndexError: return None # new article def new_post (title, text ): db. insert ('entries', title = title, content = text, posted_on = datetime. datetime. utcnow () # delete the article def del_post (id): db. delete ('entries', where = 'id = $ id', vars = locals () # modify the article def update_post (id, title, text): db. update ('entries', where = 'id = $ id', vars = locals (), title = title, content = text)
5. Create base.html#edit.html#index.html#new.html#view.html in the template directory.
<!-- base.html -->$def with (page)
6. Enter the main directory and run python blog. py in the command line. The web service will be started. Enter http: // localhost: 8080/in the browser. The simple blog is complete.