|
| 1 | +""" |
| 2 | +flask exercise |
| 3 | +""" |
| 4 | +from flask import Flask, url_for, request |
| 5 | +from flask import render_template |
| 6 | +import logging |
| 7 | +FORMAT = '%(asctime)-15s %(levelname)s %(message)s' |
| 8 | +logging.basicConfig(level=logging.INFO) |
| 9 | +logger = logging.getLogger('flask') |
| 10 | +handler = logging.FileHandler('flask.log') |
| 11 | +fmt = logging.Formatter(FORMAT) |
| 12 | +handler.setFormatter(fmt) |
| 13 | +logger.addHandler(handler) |
| 14 | + |
| 15 | +# from logging.config import dictConfig |
| 16 | +# |
| 17 | +# dictConfig({ |
| 18 | +# 'version': 1, |
| 19 | +# 'formatters': {'default': { |
| 20 | +# 'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s', |
| 21 | +# }}, |
| 22 | +# 'handlers': {'wsgi': { |
| 23 | +# 'class': 'logging.StreamHandler', |
| 24 | +# 'stream': 'ext://flask.logging.wsgi_errors_stream', |
| 25 | +# 'formatter': 'default' |
| 26 | +# }}, |
| 27 | +# 'root': { |
| 28 | +# 'level': 'INFO', |
| 29 | +# 'handlers': ['wsgi'] |
| 30 | +# } |
| 31 | +# }) |
| 32 | + |
| 33 | +app = Flask(__name__) |
| 34 | + |
| 35 | + |
| 36 | +# 路由 |
| 37 | +# @app.route('/') |
| 38 | +# def index(): |
| 39 | +# return 'index' |
| 40 | + |
| 41 | + |
| 42 | +# @app.route('/hello') |
| 43 | +# def hello(): |
| 44 | +# return 'hello world!' |
| 45 | + |
| 46 | + |
| 47 | +@app.route('/post/<int:post_id>') |
| 48 | +def show_post(post_id): |
| 49 | + return 'Post {}'.format(post_id) |
| 50 | + |
| 51 | + |
| 52 | +@app.route('/path/<path:subpath>') |
| 53 | +def show_subpath(subpath): |
| 54 | + return 'Subpath: {}'.format(subpath) |
| 55 | + |
| 56 | + |
| 57 | +""" |
| 58 | +转换器类型: |
| 59 | + string:缺省值,接受任何不包含斜杠的文本 |
| 60 | + int: |
| 61 | + float: |
| 62 | + path:类似string,可以接受斜杠 |
| 63 | + uuid:接受UUID字符串 |
| 64 | +""" |
| 65 | + |
| 66 | + |
| 67 | +@app.route('/projects/') |
| 68 | +def projects(): |
| 69 | + return 'The project page' |
| 70 | + |
| 71 | + |
| 72 | +@app.route('/about') |
| 73 | +def about(): |
| 74 | + return 'The about page' |
| 75 | + |
| 76 | + |
| 77 | +""" |
| 78 | +/ 重定向行为: |
| 79 | + projects的URL,会自动补齐尾部'/'; |
| 80 | + about的url,如果访问时尾部带了'/',会报错; |
| 81 | +""" |
| 82 | + |
| 83 | + |
| 84 | +@app.route('/') |
| 85 | +def index(): |
| 86 | + return 'index' |
| 87 | + |
| 88 | + |
| 89 | +@app.route('/login') |
| 90 | +def login(): |
| 91 | + return 'login' |
| 92 | + |
| 93 | + |
| 94 | +@app.route('/user/<username>') |
| 95 | +def profile(username): |
| 96 | + # app.logger.info('user: {}'.format(username)) |
| 97 | + logger.info('user: {}'.format(username)) |
| 98 | + return 'User %s' % username |
| 99 | + |
| 100 | + |
| 101 | +""" |
| 102 | +url_for()函数用于构建指定函数的URL。它把函数名称作为第一个参数,可以接受任意个关键字参数,每个关键字参数对应URL中的变量。 |
| 103 | +未知变量讲添加到URL中作为查询参数。 |
| 104 | +""" |
| 105 | +with app.test_request_context(): |
| 106 | + print(url_for('index')) |
| 107 | + print(url_for('login')) |
| 108 | + print(url_for('login', next='/')) |
| 109 | + print(url_for('profile', username='John Doe')) |
| 110 | + |
| 111 | + |
| 112 | +@app.route('/method', methods=['GET', 'POST']) |
| 113 | +def method(): |
| 114 | + if request.method == 'POST': |
| 115 | + return 'do post' |
| 116 | + else: |
| 117 | + return 'do show' |
| 118 | + |
| 119 | + |
| 120 | +# 如果支持GET方法,flask会自动添加HEAD方法支持; |
| 121 | +# url_for('static', filename='style.css') |
| 122 | + |
| 123 | + |
| 124 | +# flask使用Jinja2模板引擎,render_template方法可以渲染模板 |
| 125 | +# flask会在templates文件夹内寻找模板 |
| 126 | +@app.route('/hello/') |
| 127 | +@app.route('/hello/<name>') |
| 128 | +def hello(name=None): |
| 129 | + return render_template('hello.html', name=name) |
| 130 | + |
| 131 | + |
| 132 | +# error page |
| 133 | +@app.errorhandler(404) |
| 134 | +def page_not_found(error): |
| 135 | + return render_template('page_not_found.html'), 404 |
0 commit comments