How can I get current base URI in flask?
本问题已经有最佳答案,请猛点这里访问。
在下面的代码中,我想将URL存储在变量中以检查发生URL错误的错误。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @app.route('/flights', methods=['GET']) def get_flight(): flight_data= mongo.db.flight_details info = [] for index in flight_data.find(): info.append({'flight_name': index['flight_name'], 'flight_no': index['flight_no'], 'total_seat': index['total_seat'] }) if request.headers['Accept'] == 'application/xml': template = render_template('data.xml', info=info) xml_response = make_response(template) xml_response.headers['Accept'] = 'application/xml' logger.info('sucessful got data') return xml_response elif request.headers['Accept'] == 'application/json': logger.info('sucessful got data') return jsonify(info) |
输出:
1 2 3 4 5 | * Restarting with stat * Debugger is active! * Debugger PIN: 165-678-508 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [28/Mar/2017 10:44:53]"GET /flights HTTP/1.1" 200 - |
我要此消息
1 | "127.0.0.1 - - [28/Mar/2017 10:44:53]"GET /flights HTTP/1.1" 200 -" |
应该存储在变量中,或者如何获取正在执行的当前URL?
您可以在烧瓶的
1 2 3 4 5 6 7 8 9 10 11 | from flask import Flask, request app = Flask(__name__) @app.route('/foo') def index(): return request.base_url if __name__ == '__main__': app.run() |
如果应用程序路由为
1 | http://localhost:5000/foo |
使用