python基础项目教程_Python基础教程__项目(公告板)

由于最近学习Python,从最基础的Python基础教程学起,其中最后的十个项目还是很不错的。个人认为。本人新手,如有错误,还请指教。

书上用的是PostgreSQL,这里用的是MySQL。由于这是一个CGI项目。所以事先需要准备一个可以运行CGI脚本的试验环境。

本次用的是Apache运行cgi。配置网上很多。

其次需要创建一个数据表:CREATE TABLE `messages` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`subject` varchar(100) NOT NULL,

`sender` varchar(15) NOT NULL,

`reply_to` int(11) DEFAULT NULL,

`text` mediumtext NOT NULL,

PRIMARY KEY (`id`)

)

一个简单的公告板,主要功能:显示所有公告、查看单个公告、编辑公告、保存公告、删除公告(个人增加的)。根据这些功能可以创建五个对应的脚本:main.cgi、view.cgi、edit.cgi、save.cgi、delete.cgi

一、下面废话省略,直接上代码和运行图片:

1、main.cgi#!/usr/bin/python

print 'Content-type: text/html\n'

import cgitb; cgitb.enable()

import MySQLdb

conn=MySQLdb.connect(host='localhost',user='root', db='blog')

curs=conn.cursor()

print """

The Blog Bulletin

The Blog Bulletin

"""

curs.execute('select * from messages')

rows=curs.fetchall()

toplevel=[]

children={}

for row in rows:

parent_id=row[3]

if parent_id is None:

toplevel.append(row)

else:

children.setdefault(parent_id,[]).append(row)

def format(row):

print '

%s | Delete

' % (row[0],row[1],row[0])

try: kids=children[row[0]]

except KeyError: pass

else:

print '

'

for kid in kids:

format(kid)

print '

'

print '

'

for row in toplevel:

format(row)

print """


Post Message

"""

2、view.cgi#!/usr/bin/python

print 'Content-type: text/html\n'

import cgitb; cgitb.enable()

import MySQLdb

conn=MySQLdb.connect(host='localhost',user='root',db='blog')

curs=conn.cursor()

import cgi,sys

form=cgi.FieldStorage()

id=form.getvalue('id')

print """

View Message

View Mesage

"""

try: id = int(id)

except:

print 'Invalid message ID'

sys.exit()

curs.execute('select * from messages where id=%i' % id)

rows=curs.fetchall()

if not rows:

print 'Unknown message ID'

sys.exit()

row=rows[0]

print """

Subject: %s

Sender: %s

%s

Back Home

| Reply

""" % (row[1],row[2],row[4],row[0])

3、edit.cgi#!/usr/bin/python

print 'Content-type: text/html\n'

import cgitb; cgitb.enable()

import MySQLdb

conn=MySQLdb.connect(host='localhost', user='root', db='blog')

curs=conn.cursor()

import cgi, sys

form=cgi.FieldStorage()

reply_to=form.getvalue('reply_to')

print """

Compose Message

Compose Message

"""

subject=''

if reply_to is not None:

print '' % reply_to

curs.execute('select subject from messages where id=%s' % reply_to)

subject=curs.fetchone()[0]

if not subject.startswith('Re: '):

subject='Re: '+ subject

print """

Subject:

Sender:

Message:


Back Home

""" % subject

4、save.cgi#!/usr/bin/python

print 'Content-type: text/html\n'

import cgitb; cgitb.enable()

def quote(string):

if string:

return string.replace("'", "\\'")

else:

return string

import MySQLdb

conn=MySQLdb.connect(host='localhost', user='root', db='blog')

curs=conn.cursor()

import cgi, sys

form=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, 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 """

Message Saved

Message Saved


Back Home

s

"""

5、delete.cgi#!/usr/bin/python

print 'Content-type: text/html\n'

import cgitb; cgitb.enable()

import MySQLdb

conn=MySQLdb.connect(host='localhost', user='root', db='blog')

curs=conn.cursor()

import cgi, sys

form=cgi.FieldStorage()

id=form.getvalue('id')

print """

Delete Page

Delete Page!

"""

try: id = int(id)

except:

print "Invalid message ID."

sys.exit()

print """

Delete subject object successful.

"""

curs.execute('delete from messages where id=%i' % id)

conn.commit()

print """


Back Home

"""

二、运行截图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值