Skip to content
Myr

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

5 files+65 added−1 removed

modified:docs/api.rst+2 added
226 unmodified lines
227 :param code: the HTTP error code.228229.. autofunction:: redirect230231.. autofunction:: make_response232233.. autofunction:: send_file234235.. autofunction:: send_from_directory236
117 unmodified lines
modified:flask/__init__.py+1 added-1 removed
17 unmodified lines
18from .app import Flask, Request, Response19from .config import Config20from .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_response23from .globals import current_app, g, request, session, _request_ctx_stack24from .module import Module25from .templating import render_template, render_template_string26
3 unmodified lines
modified:flask/helpers.py+42 added
99 unmodified lines
100 return current_app.response_class(json.dumps(dict(*args, **kwargs),101 indent=None if request.is_xhr else 2), mimetype='application/json')102103104def 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.110111 If view looked like this and you want to add a new header::112113 def index():114 return render_template('index.html', foo=42)115116 You can now do something like this::117118 def index():119 response = make_response(render_template('index.html', foo=42))120 response.headers['X-Parachutes'] = 'parachutes are cool'121 return response122123 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::126127 response = make_response(render_template('not_found.html', 404))128129 Internally this function does the following things:130131 - 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.136137 .. 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)144145146def 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 lines
modified:tests/flask_tests.py+18 added
391 unmodified lines
392 assert rv.headers['X-Foo'] == 'Testing'393 assert rv.status_code == 400394 assert rv.mimetype == 'text/plain'395396 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'403404 rv = flask.make_response('Awesome')405 assert rv.status_code == 200406 assert rv.data == 'Awesome'407 assert rv.mimetype == 'text/html'408409 rv = flask.make_response('W00t', 404)410 assert rv.status_code == 404411 assert rv.data == 'W00t'412 assert rv.mimetype == 'text/html'413414 def test_url_generation(self):415 app = flask.Flask(__name__)416 @app.route('/hello/<name>', methods=['POST'])417 def hello():
630 unmodified lines
modified:CHANGES+2 added
24 unmodified lines
25 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.3132Version 0.5.233-------------34
106 unmodified lines