Added make_response
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent e20534d · Browse files · 5 files·+65 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+65 added−1 removed
- tests:+18 added−0 removed
- other:+47 added−1 removed
Provenance
- Commit
6fc1492- Tree
4ff5192- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
e20534d
5 files changed+65 added−1 removed
modified:docs/api.rst+2 added
226 unmodified lines227 :param code: the HTTP error code.228 229 .. autofunction:: redirect230 231+.. autofunction:: make_response232+233 .. autofunction:: send_file234 235 .. autofunction:: send_from_directory236 117 unmodified linesmodified:flask/__init__.py+1 added-1 removed
17 unmodified lines18 from .app import Flask, Request, Response19 from .config import Config20 from .helpers import url_for, jsonify, json_available, flash, \21 send_file, send_from_directory, get_flashed_messages, \22- get_template_attribute22+ get_template_attribute, make_response23 from .globals import current_app, g, request, session, _request_ctx_stack24 from .module import Module25 from .templating import render_template, render_template_string26 3 unmodified linesmodified:flask/helpers.py+42 added
99 unmodified lines100 return current_app.response_class(json.dumps(dict(*args, **kwargs),101 indent=None if request.is_xhr else 2), mimetype='application/json')102 103 104+def make_response(*args):105+ """Sometimes it is necessary to set additional headers in a view. Because106+ views do not have to return response objects but can return a value that107+ is converted into a response object by Flask itself, it becomes tricky to108+ add headers to it. This function can be called instead of using a return109+ and you will get a response object which you can use to attach headers.110+111+ If view looked like this and you want to add a new header::112+113+ def index():114+ return render_template('index.html', foo=42)115+116+ You can now do something like this::117+118+ def index():119+ response = make_response(render_template('index.html', foo=42))120+ response.headers['X-Parachutes'] = 'parachutes are cool'121+ return response122+123+ This function accepts the very same arguments you can return from a124+ view function. This for example creates a response with a 404 error125+ code::126+127+ response = make_response(render_template('not_found.html', 404))128+129+ Internally this function does the following things:130+131+ - if no arguments are passed, it creates a new response argument132+ - if one argument is passed, :meth:`flask.Flask.make_response`133+ is invoked with it.134+ - if more than one argument is passed, the arguments are passed135+ to the :meth:`flask.Flask.make_response` function as tuple.136+137+ .. versionadded:: 0.6138+ """139+ if not args:140+ return current_app.response_class()141+ if len(args) == 1:142+ args = args[0]143+ return current_app.make_response(args)144+145+146 def url_for(endpoint, **values):147 """Generates a URL to the given endpoint with the method provided.148 The endpoint is relative to the active module if modules are in use.149 315 unmodified linesmodified:tests/flask_tests.py+18 added
391 unmodified lines392 assert rv.headers['X-Foo'] == 'Testing'393 assert rv.status_code == 400394 assert rv.mimetype == 'text/plain'395 396+ def test_make_response(self):397+ app = flask.Flask(__name__)398+ with app.test_request_context():399+ rv = flask.make_response()400+ assert rv.status_code == 200401+ assert rv.data == ''402+ assert rv.mimetype == 'text/html'403+404+ rv = flask.make_response('Awesome')405+ assert rv.status_code == 200406+ assert rv.data == 'Awesome'407+ assert rv.mimetype == 'text/html'408+409+ rv = flask.make_response('W00t', 404)410+ assert rv.status_code == 404411+ assert rv.data == 'W00t'412+ assert rv.mimetype == 'text/html'413+414 def test_url_generation(self):415 app = flask.Flask(__name__)416 @app.route('/hello/<name>', methods=['POST'])417 def hello():630 unmodified linesmodified:CHANGES+2 added
24 unmodified lines25 new ``MAX_CONTENT_LENGTH`` configuration value.26 - the endpoint for the :meth:`flask.Module.add_url_rule` method27 is now optional to be consistent with the function of the28 same name on the application object.29+- added a :func:`flask.make_response` function that simplifies30+ creating response object instances in views.31 32 Version 0.5.233 -------------34 106 unmodified lines