android开发技巧flask程序员

二、mvc完整处理过程

2017-10-16  本文已影响66人  扑腾的蛾子

站点结构

入口类 : flask类的实例、@app.route('/')路由装饰响应处理函数、程序配置 app.debug=True、程序运行

site->static(images、js、css)、templates(index.html)

结构 mvc

请求/响应流程

路由接收请求

分配请求给特定的视图函数

视图函数返回视图

mvc流程示例

整体文件结构,没有展开的就是空文件,以这个目录为参考完成下面的示例

1、简单的vc模式

@app.route('/')

def index():

    return render_template('index.html')

hello.py文件

index.html页面,注意/static默认是从根目录去找static文件夹

body{

font-size: 16;

color: #333;

}

h1,h2,h3{

font-weight: 300;

}

style.css文件

2、mvc结构

class Book:

    def __init__(self, title, price, author, publisher):

            self.title = title

            self.price = price

            self.author = author

            self.publisher = publisher

    def __str__(self):

        return '<Book {}>'.format(self.title)

book.py文件

@app.route('/books/')

def book_list():

    books = [

        Book('python flask', 59.00, 'dong', '人民邮电出版社'),

        Book('python selenium', 59.00, 'dong', '人民邮电出版社'),

        Book('python 爬虫', 59.00, 'dong', '人民邮电出版社'),

        Book('python 多线程', 59.00, 'dong', '人民邮电出版社'),

        Book('python 语言', 59.00, 'dong', '人民邮电出版社')

    ]

    return render_template('book-list.html', books=books)

hello.py文件

book-list.html

今天是一个整体的示例,后面将会有对各个部分的讲解,大家了解整体的流程就可以了。后面的开发也都会大体遵循这种流程模式的。

上一篇下一篇

猜你喜欢

热点阅读