Python Basic Tutorial __ Project (bulletin board)

Source: Internet
Author: User

With the recent learning of Python, the last 10 projects are good, from the basics of the basic Python tutorial. Personally think. I am a novice, if there is a mistake, please advise.

The book uses PostgreSQL, which uses MySQL. Because this is a CGI project. So you need to prepare a test environment that can run CGI scripts beforehand.

This time using Apache to run CGI. There are many configurations online.

Next you need to create a data table:

CREATE TABLE ' Messages ' (' ID ' int (one) not NULL auto_increment, ' subject ' varchar (+) ' NOT null, ' sender ' varchar (N OT null, ' reply_to ' int (one) DEFAULT null, ' text ' mediumtext not NULL, PRIMARY KEY (' id ')) engine=innodb auto_increment= DEFAULT Charset=utf8

A simple bulletin board, main features: Show all announcements, view individual announcements, edit announcements, save announcements, delete announcements (personal additions). Based on these features, you can create five scripts: main.cgi, view.cgi, edit.cgi, save.cgi, delete.cgi

One, the following nonsense omitted, directly on the code and run the picture:

1, main.cgi

#!/usr/bin/pythonprint  ' content-type: text/html\n ' import cgitb; cgitb.enable () import  Mysqldbconn=mysqldb.connect (host= ' localhost ', user= ' root ',  db= ' blog ') curs=conn.cursor () print  "" "< Html>  

2, view.cgi

#!/usr/bin/pythonprint  ' content-type: text/html\n ' import cgitb; cgitb.enable () import  Mysqldbconn=mysqldb.connect (host= ' localhost ', user= ' root ', db= ' blog ') curs=conn.cursor () import cgi,sysform= Cgi. Fieldstorage () id=form.getvalue (' id ') print  "" "

3, edit.cgi

#!/usr/bin/pythonprint  ' content-type: text/html\n ' import cgitb; cgitb.enable () import  Mysqldbconn=mysqldb.connect (host= ' localhost ',  user= ' root ',  db= ' blog ') curs=conn.cursor () import  cgi, sysform=cgi. Fieldstorage () reply_to=form.getvalue (' reply_to ') print  "" "

4, save.cgi

#!/usr/bin/pythonprint  ' content-type: text/html\n ' import cgitb; cgitb.enable () def  Quote (string):     if string:        return  string.replace ("'",  "\ \")     else:         return stringimport mysqldbconn=mysqldb.connect (host= ' localhost ',  user= ' root ',  db= ' Blog ') curs=conn.cursor () import cgi, sysform=cgi. Fieldstorage () sender=quote (Form.getvalue (' sender ')) Subject=quote (Form.getvalue (' subject ')) Text=quote ( Form.getvalue (' text ')) reply_to=form.getvalue (' reply_to ') if not  (sender and subject  And text):    print  ' plz supply sender, subject, and  Text '     sys.exit () if reply_to is not none:     query =  "" "    insert into messages (REPLY_TO,&NBsp;sender, subject, text)     value (%i,  '%s ',  '%s ',  '%s ') "" "  %  (int (reply_to),  sender, subject, text) else:    query =   "" "    insert into messages (Sender, subject, text)      value ('%s ',  '%s ',  '%s ') ' "" " %  (Sender, subject, text) Curs.execute ( Query) Conn.commit () print  "" "

5, delete.cgi

#!/usr/bin/pythonprint  ' content-type: text/html\n ' import cgitb; cgitb.enable () import  Mysqldbconn=mysqldb.connect (host= ' localhost ',  user= ' root ',  db= ' blog ') curs=conn.cursor () import  cgi, sysform=cgi. Fieldstorage () id=form.getvalue (' id ') print  "" "


Second, the operation

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/54/93/wKioL1SGu8WAqcUVAAD3dHLRbOw210.jpg "title=" 1.png " alt= "Wkiol1sgu8waqcuvaad3dhlrbow210.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/54/93/wKioL1SGu9fghd5PAADtRNGM4RI193.jpg "title=" 2.png " alt= "Wkiol1sgu9fghd5paadtrngm4ri193.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/93/wKioL1SGu-OjxOm8AADKVDj7zvQ555.jpg "title=" 3.png " alt= "Wkiol1sgu-ojxom8aadkvdj7zvq555.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/95/wKiom1SGu1ihjEjWAAC6zDmKTFw678.jpg "title=" 4.png " alt= "Wkiom1sgu1ihjejwaac6zdmktfw678.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/93/wKioL1SGvKvSmb2jAADNb2KBbsw350.jpg "title=" 1.jpg " alt= "Wkiol1sgvkvsmb2jaadnb2kbbsw350.jpg"/>


This article is from the "Soul" blog, make sure to keep this source http://chenpipi.blog.51cto.com/8563610/1588007

Python Basic Tutorial __ Project (bulletin board)

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.