WEB Python lightweight framework web.py, template

First, the web.py installation, there are several methods, as follows

pip install web.py==0.40

easy_install web.py
again after installation or downloading http://webpy.org/install.zh-cn
\ the Users \ Tony \ Downloads \ webpy-0.40: C CD
is mounted to the directory after decompression is positioned
python setup.py install


The WARNING: PIP by You are the using Version 19.1.1, 19.3.1 Version HOWEVER IS Available.
By You Should the Consider Upgrading Via The 'python -m pip install --upgrade pip' Command.
If the python -m pip install --upgrade pip not upgrade success following statement
python -m pip install --upgrade pip --user

Pip error after upgrading
TypeError: 'module' object is not callable

Delete
python -m pip uninstall pip

After a successful test
python test.py
default port 8080, or the port, such as python test.py 9999, port 9999
HTTP: // localhost: 9999

After the installation finished, started a web page, use the following three ways of testing, no template, template, template nesting

#!/usr/bin/python
# coding=gbk
import web
#第一部分正则表达式,第二部分接受请求的类名
urls=(
    '/i/(.*)', 'index1',        
    '/m/(.*)','index2',   
    '/c/(.*)','index3',      
) 

app=web.application(urls, globals())
T_tony=web.template.frender("templates/m_tony.html")  #新建templates目录,存放模板文件
C_tony=web.template.render("templates",base="m_common") #使用公用模板m_common.html


#未使用模板
class index1:
    def GET(self, nickname):
        print('你好:', nickname+'!')      #命令行显示
        return 'Hello, ' + nickname + '!'  #web页面显示

#使用模板
class index2:
    def GET(self,nname):
        return T_tony('TonyTitle',nname)

#m_index.html模板,来嵌套m_common.html模板
class index3:
    def GET(self,nname):
        return C_tony.m_index(nname)


if __name__ == "__main__":
    app.run()

 m_tony.html

$def with(tname,name1)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>$tname</title>  
</head>
<body>
My Friend $name1
</body>
</html>

m_index.html

$def with(fname)
$var t:This is m_index.html
Hi $fname

m_common.html

$def with(msg)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>$msg.t</title>  
</head>
<body>
$msg
</body>
</html>

The last run of results:

test.py 9999 Python
HTTP: // localhost: 9999 / i / Tony
Web page will display Hello, Tony!


HTTP: // localhost: 9999 / m / Tony
the Title page is: TonyTitle
content is: My Friend Tony
View page source is m_tony.html presentation template


HTTP: // localhost: 9999 / c / Tony
the Title page is: This is m_index.html
content: Hi Tony
View Page Source may know, m_common.html public template is embedded entered, in which the variables m_index.html It can also be used m_common.html

Published 46 original articles · won praise 9 · views 3641

Guess you like

Origin blog.csdn.net/weixin_41896770/article/details/103267221