RWPy4learner 11.3.30 documentation

Version: 11.3.30

人性化

“添加后连个自动转向都没有!什么破东西!”

模板系统

在bottle自带的模板中,小白尝试了一下传进去一个<br />换行符等HTML标签

结果都变成纯文本传出来了……没有得到换行符的结果。怎么能够让模板传入HTML代码,使模板功能更将大呢? 小白在网上搜索了一下“python 模板”,搜出一大堆结果。小白发现Python有以下许多模板:

小白继续看,发现小而强大的模板系统就是—— Mako 。“嗯!试试mako吧。先下把mako安装了再说。”

安装:

sudo easy_install mako

改进模板

白板型的模板连小白自己都看不下去了,用CSS!

Leo

夫 `Leo`_ 者,文学编程 之环境也!

在工具篇里有具体介绍, 也是听从 行者 建议,上了瘾的奇妙工具...

用clone节点来记录css,方便了许多,减少重复。

../_images/malaweb_leo.png

See also

(^.^)

  • 但是,这里,小白是什么动机开始使用 Leo 了呢,又怎么学会的呢?
  • 和其它编辑环境相比有什么好处和不足? 在 M$ 环境中有什么问题?

CSS

夫CSS者,样式表也!

有兴趣的小白可以到网上搜搜,可以搜出一大堆教程

<style type="text/css">

h1{
  color : grey;
  font-size : 27px;
}
a{
  color : clack;
  font-size : 10pt;
}
</style>

再连同title等标签和导航栏写成一个head节点,clone到每一个页面:

# -*- coding: utf-8 -*-
<html>
<head>
<title>mala通讯录</title>
  <style type="text/css">
  h1{
    color : grey;
    font-size : 27px;
  }
  a{
    color : clack;
    font-size : 10pt;
  }
  </style>
</head>
<body>
<h1>通讯录程序</h1>
<br />
<hr />
<a href="add">添加</a> | <a href="view">搜索</a>

然后一个个写页面

add.tpl 添加页面模板

# -*- coding: utf-8 -*-
<html>
<head>
<title>mala通讯录</title>
<style type="text/css">
h1{
color : grey; 
font-size : 27px;
}
a{
color : clack;
font-size : 10pt;
}
</style>
</head>
<body>
<h1>通讯录程序</h1>
<br />
<hr />
<a href="add">添加</a> | <a href="view">搜索</a>
  <form name="add" id="add"  method="post"> 
   First name: 
  <input type="text" name="firstname" /> 
  <br /> 
  Last name: 
  <input type="text" name="lastname" /> 
  <br/> 
  Address:
  <input type="text" name="address" /> 
  <br/> 
  Phone:
  <input type="text" name="phone"> 
  <br/> 
  E-mail:
  <input type="text" name="email"> 
  <br/> 
  <input type="submit" value="Submit" /> 
  </form> 
<hr/>
created by QidongSu
<br/>
powerded by <a href="http://www.python.org/">Python</a>
;<a href="http://webpages.charter.net/edreamleo/front.html">Leo</a>
;<a href="http://bottlepy.org/">Bottle</a>
;<a href="http://www.makotemplates.org/">Mako</a>
</body>
</html>

view.tpl 搜索页面模板

# -*- coding: utf-8 -*-
<html>
<head>
<title>mala通讯录</title>
<style type="text/css">
h1{
color : grey; 
font-size : 27px;
}
a{
color : clack;
font-size : 10pt;
}
</style>
</head>
<body>
<h1>通讯录程序</h1>
<br />
<hr />
<a href="add">添加</a> | <a href="view">搜索</a>
  <form name="search" id="search"  method="get">
   KeyText: 
  <input type="text" name="keytext" />
  <br />
  <input type="submit" value="Submit" />
  </form>
${html} 
<hr/>
created by QidongSu
<br/>
powerded by <a href="http://www.python.org/">Python</a>
;<a href="http://webpages.charter.net/edreamleo/front.html">Leo</a>
;<a href="http://bottlepy.org/">Bottle</a>
;<a href="http://www.makotemplates.org/">Mako</a>
</body>
</html>

Done!

../_images/newadd.png

view页面

../_images/newview.png

自动跳转

bottle框架中的自动定向跳转语句:

bottle.redirect('/view')

添加到POST的add函数中

搜索

but,搜索功能还没有完成呢,不过,这对小白来说也不是什么难事,修改view函数:

@bottle.route('/view')
def view():
    printlist=['firstname',
    'lastname',
    'phone',
    'email',
    'address']
    has_keywd='keytext' in bottle.request.GET
    if has_keywd:
        keywd=bottle.request.GET['keytext']
    else:
        keywd=""
    string=""
    if not has_keywd:
        for i in db.keys():
            for j in printlist:
                string+=j+": "+db[i][j]+r'<br />'
            string+=r"<br />"
    else:
        list_=[]
        for i in db.keys():
            for j in printlist:
                if keywd in db[i][j]:
                    print keywd
                    list_.append(i)
        for i in list_:
            for j in printlist:
                string+=j+": "+db[i][j]+r'<br />'
    tmpl = Template(filename='./views/view.tpl', output_encoding='utf-8')
    return tmpl.render(html=string)

只是单纯的用字典的操作方法来操作而已……虽然代码长了一些,但是也能用了!

结局

嗯嗯嗯,不错不错,终于能够使用了……