Python's simple blog based on web.py

Source: Internet
Author: User

Initialize Init.sql:

CREATE TABLE Blog (
    ID INT auto_increment,
    title text,
    content TEXT,
    posted_on DATETIME,
    Primary Key (ID)
);

CREATE TABLE COMMENT (
     ID int auto_increment,
     content TEXT,
     posted_on DATETIME,
     blog_id int,
     Primary key (ID)
     );


my_blog.py

#!
    /usr/bin/python # encoding:utf-8 Import Web import Model # URL Map 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, ' Cookie ': 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 log in = Web.form.Form (  Web.form.Textbox (' username '), Web.form.Password (' Password '), Web.form.Button (' login ')) # Home class classes Index:def Get (self): Login_form=login () posts=model.get_posts () return Render.index (posts,login_form) d  EF 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/Comment Article Class, Class View (): Form=web.form.form (Web.form.Textarea) (' Comments ', W Eb.form.notnull, Rows=5, cols=40, description= ' Comment Contents: '), Web.form.Button (' commit ') def get (Self,id): Form=self.form () print ID post = model.get_post (int (id)) comments=model.get_comments (int (ID)) return Render.view (post,com Ments,form def POST (self,id): Form=self.form () post=model.get_post (int (id)) comments=model.ge t_comments (int (ID)) if not form.validates (): Return Render.view (Post,comments,form) model.new_ Comment (form.d.comments,id) comments_new=model.get_comments (int (ID)) return Render.view (Post,comments_new, form) # New article Class New:form=web.form.form (Web.form.Textbox (' title '), Web.form.notn
                   Ull,      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 article class Delete:def Get (Self,id): model.del_post (int (id)) Raise Web.seeother ('/') # Edit article Class classes Edit:def Get (Self,id): post=model.get_post (int (id)) Form=new.form () Form.fill (POST) return RENDER.E DIT (post,form) def post (Self,id): Form=new.form () post=model.get_post (int (ID)) if not form.val Idates (): RETurn Render.edit (post,form) model.update_post (int (id), form.d.title,form.d.content) Raise Web.seeother ('/')  # Exit Login class Logout:def get (self): Web.setcookie (' username ', ', ', expires=-1) raise Web.seeother ('/') #

Define 404 Def NotFound (): Return Web.notfound ("Sorry,the page, were looking for is not found") App.notfound=notfound

 # run If __name__ = = ' __main__ ': App.run ()
model.py:

#! /USR/BIN/PYTHPN # encoding:utf-8 Import Web import datetime # Database connection db=web.database (dbn= ' MySQL ', host= ' 192.168.48.10 ',  port=3306,db= ' Test ', user= ' test ', pw= ' MySQL ') # get all articles Def get_posts (): Return db.select (' blog ', order= ' id ') # Get article content def
        Get_post (ID): Try:return db.select (' blog ', where = ' id= $id ', Vars=locals ()) [0] except Indexerror: 
    Return None # Get article Comment def get_comments (id): Try:return db.select (' comment ', where= ' blog_id= $id ', Vars=locals ()) Except Indexerror:return None # new Comment def new_comment (comment,id): Db.insert (' comment ', con Tent=comment, Posted_on=datetime.datetime.utcnow (), Blog_id=id) # new article Def new_post (Title,text ): Db.insert (' blog ', Title=title, Content=text, Posted_on=datetime.datetime. UtcNow ()) # Delete Article def del_post (ID): db.delete (' blog ', where= ' id = $id ', vars=locals ()) #修改文章 def update_post (ID, title, Text): Db.update (' BloG ', where = ' id = $id ', VARs = Locals (), title = title, content = text) 

HTML templates:

Base.html:

$def with (page)
Index.html

$def with (posts, login_form)

View.html

$def with (post,comments,form)
New.html:

$def with (form)

Edit.html:
$def with (post, form)


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.