web 框架

#coding=utf-8
__author__ = 'mac'

#web框架做前端开发
#web 框架的结构可以单独拿出来:(1)init_sql脚本是初始化表结构 (2)入口文件(__main__,导入url,函数处理方法等的导入) (3)url单独拿出来
# (4)函数的处理(各个函数的方法)
# (5)setting文件(例如定义模板目录,定义数据库,创建数据库引擎,定义数据库的错误返回如若HTTPError继续提交,数据库错误rollback
#      session存储在数据库的存储方式),(6)数据库模型(表结构,eg:posttime=Colum(DateTime))

#请求参数获取:web.input()
#请求头获取:web.ctx.env

"""
响应处理:
(1)模板文件读取:render.index("参数")
 (2) 结果数据获取:model.select("sql")
 (3) URL跳转:web.seeother("/")
"""

import web
import MySQLdb
import MySQLdb.cursors


#使用模板文件,模板文件入口
#使用模板文件2步:1.是引入模板文件夹,2,是return render(hello2.html) 模板文件夹下面的文件 3.模板中传入参数的话,在模板中引用参数
render=web.template.render('/Users/mac/PycharmProjects/python2.7/webFrame/templates') #参数是模板文件的路径

#匹配url,用正则表示
urls=(
    '/article','article',
    '/index','index',
    '/blog/\d+','blog', #/blog/任意数字,都是调blog函数
    '/(.*)','hello', #除了上面两种其它的都是匹配hello函数
)

app=web.application(urls,globals())

class index:
    def GET(self):
        #请求参数获取
        # query=web.input()
        # return query  #将参数返回到页面上
        return web.seeother('http://www.baidu.com')  #跳转到别的用页页

class blog:
    def POST(self):
        #请求POST获取参数
        data=web.input()
        return data
    def GET(self):
        return web.ctx.env #获取get的请求头参数,header的一些信息
class hello:
     def GET(self,name):
         # return open(r'/Users/mac/PycharmProjects/python2.7/webFrame/1.html').read() #返恩1.html网页,并读取它内容
        # return render.hello2() #此处的hello2是模板文件夹下面的模板文件的名字
        return render.he11o2(name) #用模板的好处是还可以传参数,这里传入name一个参数,然后在html中加上$def with(name),下面的input 中
        #name="username" value="$name",改成参数回写,这样就达到一个动态的效果,而open就不可以。例如在网页中传入参数:http://0.0.0.0:8081/xiaowu
        #则参数会直接显示在hello2的页面中
class article:
    def Get(self):
        conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='root',db='mydb',port=3306,cursorclass=MySQLdb.cursors.DictCursor)
        cur=conn.cursor()
        cur.execute('select * from books_book')
        r=cur.fetchall()
        cur.close()
        conn.close()
        print r
        return render.article(r) #将结果显示在article.html模板中
# a=article()
# print a.Get()

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

#备注:indext查看参数http://0.0.0.0:8081/index?name=111&age=1

# blog:http://0.0.0.0:8081/blog/123/

相关的html文件:

(1)1.html

<html>
    <head>
        <title>hello</title>>
    </head>
    <body>
        <h1>POST</h1>
        <form action="/blog/123" method="POST">
            <input type="text" name="name" value=""/>
            <input type="password" name="password" value=""/>
            <input type="submit" value="submit">
        </form>

    </body>
</html>

(2)templates中的article.html
$def with(r)
<html>
    <head>
        <meta charset="utf-8"/>
        <title>books_book</title>>
    </head>>
    <body>
        <h1>文章列表</h1>
        <ul>
        $for l in r:
            <li>$l.get('id')=>$l.get('title')</li>
        </ul>
    </body>

</html>
(3)templates中的hello2.html
$def with(name)
<html>
    <head>
        <title>hello</title>>
    </head>
    <body>
        <h1>POST</h1>
        <form action="/blog/123" method="POST">
            <input type="text" name="name" value="$name"/>
            <input type="password" name="password" value=""/>
            <input type="submit" value="submit">
        </form>

    </body>
</html>






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值