[Karrigell]Karrigell Web开发入门第二季 Part2.doc_第1页
[Karrigell]Karrigell Web开发入门第二季 Part2.doc_第2页
[Karrigell]Karrigell Web开发入门第二季 Part2.doc_第3页
[Karrigell]Karrigell Web开发入门第二季 Part2.doc_第4页
[Karrigell]Karrigell Web开发入门第二季 Part2.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

上接KarrigellKarrigell Web开发入门第二季 Part1这个CD收藏的程序分为主页和维护数据两部分,上一章节已经讲了主页的设计部分下面就来看看维护数据库部分。Database engineKarrigell可以利用适当的模块使用任何数据库引擎(MySQL,SQLite,等)。在这个例子中我们使用非常简单的模块存储数据,我们需要的数据有:收藏的每个CD,艺术家名字和CD封面的标题。拷贝如下代码到simpledb.py模块中:def read(filename): records = try: for line in open(filename): records.append(line.strip().split(#) except IOError: pass return recordsdef save(records,filename): out = open(filename,w) for items in records: out.write(#.join(items)+/n) out.close()当我们在一个脚本中import这个模块后,我们就可以简单的使用read()和save()函数了。这些数据存放在一个列表里。在我们的CD收藏程序中,信息将会包含两个值:艺术家名字和CD标题。如果我们把数据库叫做mycds.db,那么在HTML表格里打印所有CD的程序就是这样的:import simpledbcds = simpledb.read(mycds.db)for (artist,title) in cds: print artist,titleHome page with the CD list我们现在修改index()函数来打印所有CD的列表。import simpledbdef index(): print My record collection # login / logout logged = hasattr(Session(),user) and Session().user is not None if logged: print Logged in as %s %Session().user print Logout else: print Login # print existing records cds = simpledb.read(mycds.db) if cds: print print ArtistTitle for (artist,title) in cds: print %s%s %(artist, title) print else: print No CD in the collection # prompt logged in users to enter a new record if logged: print Enter new CD # page counter Include(./counter.py,counter_file=counter.txt)注意我们是在模块的最顶端import simpledb的:它的意思是可以在所有的函数里可用,就像纯粹的Python脚本的意义一样。Adding new CDs为登录用户提供一个用于在数据库中创建新CD的链接。这个链接的href属性值是new_cd,所以我们必须写个函数new_cd()这个函数将打印一个表单来输入CD的艺术家和标题,并且可以提交到另一个函数来把数据写到数据库里,然后返回到主页。这时候我们不应该看不懂如下的代码:def new_cd(): print New CD print print Artist print Title print print def insert_new_cd(artist,title): cds = simpledb.read(mycds.db) cds.append(artist,title) simpledb.save(cds,mycds.db) raise HTTP_REDIRECTION,index添加上面的函数到index.ks然后输入一组CD的信息到数据库里。每次返回到主页我们都会看到收藏的CD在增加。Editing records登录的用户应该能编辑CD的信息,应该在CD列表上显示一个“Edit链接用来编辑数据。这就需要CD的信息可以从主页到编辑页面,为此我们在链接上附加CD的信息,如:href = edit?artist=Beatles&title=Revolver;但是数据库一般提供记录标识符,在我们的例子中使用记录号来标识。在我们的这个简单数据库里我们可以使用列表的索引来标识,因此我们可以通过如下方式打印CD收藏的数据: # print existing records import simpledb cds = simpledb.read(mycds.db) if cds: print print ArtistTitle for num,(artist,title) in enumerate(cds): print %s%s %(artist, title) if logged: print Edit %num print print else: print No CD in the collection函数edit()将会接收一个叫num的参数。所有传递到Karrigell Service中的函数都是字符串形式的,因此在把num当作列表索引的时候不要忘记把它转换成整数。def edit(num): cds = simpledb.read(mycds.db) artist,title = cdsint(num) print New CD print print %num print Artist %artist print Title %title print print def update_cd(num,artist,title): cds = simpledb.read(mycds.db) cdsint(num) = (artist,title) simpledb.save(cds,mycds.db) raise HTTP_REDIRECTION,indexRemoving records最后一步是可以删除CD数据,修改index()函数: # print existing records import simpledb cds = simpledb.read(mycds.db) if cds: print print ArtistTitle if logged: print *2 print for num,(artist,title) in enumerate(cds): print %s%s %(artist, title) if logged: print Edit %num print Remove %num print print else: print No CD in the collection然后添加一个新函数,remove():def remove(num): cds = simpledb.read(mycds.db) del cdsint(num) simpledb.save(cds,mycds.db) raise HTTP_REDIRECTION,indexSummary现在我们有了一个完整的管理我们CD收藏数据的应用程序。index.ks脚本中的结构就像其它任何Python模块一样简洁:import simpledbdef index(): . # login / logout . # print existing records . # prompt logged in users to enter a new record . # page counter .def login(): .def check_login(login,passwd): .def logout(): .def new_cd(): .def insert_new_cd(artist,title): .def edit(num): .def

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论