python基础 26章 项目7 自定义电子公告板

#postgreSQL创建表

CREATE TABLE messages (
CREATE TABLE message (
id SERIAL PRIMARY KEY,
subject TEXT NOT NULL,
sender TEXT NOT NULL,
reply_to INTEGER REFERENCES message,
text TEXT  NOT NULL
);


#MySQL内创建表

CREATE TABLE message (
id INT NOT AUTO_INCREMENT,
subject VARCHAR(100) NOT NULL,
sender VARCHAR(15) NOT NULL,
reply INT,
text MEDIUMTEXT NOT NULL,
PRIMARY KEY(id)


#在SQLite中创建数据库

CREATE TEBLE message (
id integer primary key autoincrement,
subject text not null,
sender text not null,
reply_to int,
text text not null

);

--------------------------------------------------------------------------------------


#!/usr/bin/python

# *-*coding: utf-8 *-*


#main.cgi 电子公告板主页
print('Content-type:text/html\n')


import cgitb; cgitb.enable()


#import MySQLdb
#conn = MySQLdb.connect(user='', passwd='', db='', host='')
import psycopg2
conn = psycopg2.connect(user='', database='', password='', host='127.0.0.1')
curs = conn.cursor()




print("""
<html>
<head>
<title>Foobar Bulletin Board</title>
</head>
<body>
<h1>The Foobar Bulletin Boaed Page</h1>
""")



#查找和获取所有数据
curs.execute('SELECT * FROM messages')
rows = curs.fetchall()
toplevel = []
children = {}

#reply_to为空加入到toplevel,否则把当前id和[row]加入到children中
for row in rows:
parent_id = row[3]
if parent_id is None:
toplevel.append(row)
else:
children.setdefault(parent_id, []).append(row)

#输出每行的主题并链接到view,try children是否有当前行id的键,如果存在,else中对每个值调用format打印主题
def format(row):
print('<p><a href="view.py?id=%i">%s</a></p>' % (row[0], row[1]))
try: kids = children[row[0]]
except KeyError: pass
else:
print("<blockquote>")
for kid in kids:
format(kid)
print("</blockquote>")


print('<p>')

#对每个行调用format函数打印主题链接

for row in toplevel:
format(row)


print("""
</p>
<hr />
<p><a href='edit.py'>Post Messages</a></p>
</body>
</html>

""")


--------------------------------------------------

#!/usr/bin/python
# *-*coding: utf-8 *-*


#view.cgi 信息浏览
print('Content-type:text/html\n')
 
import cgitb;cgitb.enable()
 
#import MySQLdb
#conn = MySQLdb.connect(user='', passwd='', db='', host='')
import psycopg2
conn = psycopg2.connect(user='', database='', password='', host='127.0.0.1')
curs = conn.cursor()
 
import cgi, sys
form = cgi.FieldStorage()
id = form.getvalue('id')
 
print("""
<html>
<head>
<title>View Message</title>
</head>
<body>
<h1>The View Message Page</h1>
""")


try: id = int(id)
except:
print("Invalid messages ID")
sys.exit()

#将所有结果的行作为序列的序列获取
curs.execute("SELECT * FROM messages WHERE id='%i'" % id)
rows = curs.fetchall()


if not rows:
print("Unknown messages ID")
sys.exit()


row = rows[0]


print("""
<p><b>Subject: </b>%s<br />
<b>Sender: </b>%s<br />
<pre>%s</pre>
</p>
<hr />
<a href="main.py">Back to the main page</a>
| <a href="edit.py?reply_to=%s">Reply</a>
</body>
</html>
""" % (row[1], row[2], row[4], row[0]))

---------------------------------------------------------------------

#!/usr/bin/python
# *-*coding: utf-8 *-*


#26-7 edit 消息编辑
print('Content-type:text/html\n')
 
import cgitb;cgitb.enable()
 
#import MySQLdb
#conn = MySQLdb.connect(user='', passwd='', db='', host='')
import psycopg2
conn = psycopg2.connect(user='', database='', password='', host='127.0.0.1')
curs = conn.cursor()
 
import cgi,sys
form = cgi.FieldStorage()
reply_to = form.getvalue('reply_to')
 
print("""
<html>
<head>
<title>Compose Messages</title>
</head>
<body>
<h1>The Compose Messges Page</h1>

<form action='save.py' method='POST'>
""")

#如果reply_to不是None,把它保存在表单的一个隐藏输入中 type='hidden'
#subject = curs.fetchone()[0] 将一个结果作为元组获取
subject = ''
if reply_to is not None:
print('<input type="hidden" name="reply_to" value="%s">' % 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("""
<b>Subject:</b><br />
<input type='text' size='40' name='subject' value='%s'><br />
<b>Sender:</b><br />
<input type='text' size='40' name='sender' /><br />
<b>Messages:</b><br />
<textarea name='text' cols='40' rows='20'></textarea><br />
<input type='submit' name='Save' />
</form>
<hr />
<a href='main.py'>Back to the main page</a>
</body>
</html>
""" % subject)

-----------------------------------------------------------------------------------

#!/usr/bin/python
# *-*coding: utf-8 *-*
 
#save.cgi 保存消息
print("Content-type: text/html\n")n'
 
import cgitb;cgitb.enable()
 
def quote(string):
        if string:
                return string.replace("'","\\'")
        else:
                return string
 
#import MySQLdb
#conn = MySQLdb.connect(user='', passwd='', db='', host='')
import psycopg2
conn = psycopg2.connect(user='', database='', password='', host='127.0.0.1')
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 'Please supply sender,subject,text'
        sys.exit()
 
if reply_to is not None:
        query = """
        INSERT INTO messages(reply_to,sender,subject,text)
        VALUES(%d,'%s','%s','%s')""" % (int(reply_to),sender,subject,text)
else:
        query = """
        INSERT INTO messages(sender,subject,text)
        VALUES('%s','%s','%s')""" % (sender,subject,text)
 
curs.execute(query)
conn.commit()
 
print("""
<html>
<head>
<title>Messages Saved</title>
</head>
<body>
<h1>The Messages Saved Page</h1>
<hr />
<a href='main.py'>Back to the main page</a>
</body>
</html>
""")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值