zoukankan      html  css  js  c++  java
  • python flask 学习与实战

    路由的另一种表示,在类视图中会使用

    from flask import Flask, make_response
    
    app = Flask(__name__)
    def hello():
        return "Hello World!"
    
    app.add_url_rule('/hello', view_func = hello)
    if __name__ == '__main__':
        app.run(debug=True)
    

    视图函数与普通函数的区别

    这里是返回 status code, content-type(在headers中默认是text/html),所以当return ''时无回显,如下面,访问这个,会跳转到百度

    from flask import Flask, make_response
    
    app = Flask(__name__)
    def hello():
        # status code
        # content-type(http headers text/html)
        headers = {
            'content-type':'text/plain',
            'location':'http://baidu.com'
        }
        #response = make_response('<html></html>', 301)
        #response.headers = headers
        #return response
        return '<html></html>', 301, headers
        #return "<html></html>"
    def helloo():
        return "Hello World!"
    
    app.add_url_rule('/hello', view_func = hello)
    if __name__ == '__main__':
        app.run(debug=True)
    

    实战

    编写一个书籍搜索,我这里先实现搜索分类,书籍搜索含关键字和isbn编号搜索,isbn分为isbn10 isbn13 前者有'-',0-9组成,后面有0-9数字组成

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/book/search/<q>/<page>')
    def search(q, page):
        """
            q  (isbn13 0-9  isbn10'-')
            page
        """
        isbn_or_key = 'key'
        if len(q) == 13 and q.isdigit():
            isbn_or_key = 'isbn'
        short_q = q.replace('-', '')
        if '-' in q and len(short_q) == 10 and short_q.isdgit:
            isbn_or_key = 'isbn'
        pass
    if __name__ == '__main__':
        app.run()
    
  • 相关阅读:
    C#调用WebService
    在asp.net中Bind和Eval的区别详解
    详细说明WebService特性
    Remoting技术简介
    Web Service是如何工作的
    C#面试题
    创建一个简单的Web Service
    innerHTML属性导致未知的运行时错误ie bug
    一些想法:关于备份
    数据库考试中常见题分析:关系代数中的除法运算
  • 原文地址:https://www.cnblogs.com/spark-xl/p/9162077.html
Copyright © 2011-2022 走看看