Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added make_response

  • Loading branch information...
commit 6fc1492357a94ab553fe7ef16a7a2235d05f5a1d 1 parent e20534d
@mitsuhiko authored
View
2  CHANGES
@@ -26,6 +26,8 @@ Release date to be announced, codename to be decided.
- the endpoint for the :meth:`flask.Module.add_url_rule` method
is now optional to be consistent with the function of the
same name on the application object.
+- added a :func:`flask.make_response` function that simplifies
+ creating response object instances in views.
Version 0.5.2
-------------
View
2  docs/api.rst
@@ -228,6 +228,8 @@ Useful Functions and Classes
.. autofunction:: redirect
+.. autofunction:: make_response
+
.. autofunction:: send_file
.. autofunction:: send_from_directory
View
2  flask/__init__.py
@@ -19,7 +19,7 @@
from .config import Config
from .helpers import url_for, jsonify, json_available, flash, \
send_file, send_from_directory, get_flashed_messages, \
- get_template_attribute
+ get_template_attribute, make_response
from .globals import current_app, g, request, session, _request_ctx_stack
from .module import Module
from .templating import render_template, render_template_string
View
42 flask/helpers.py
@@ -101,6 +101,48 @@ def get_current_user():
indent=None if request.is_xhr else 2), mimetype='application/json')
+def make_response(*args):
+ """Sometimes it is necessary to set additional headers in a view. Because
+ views do not have to return response objects but can return a value that
+ is converted into a response object by Flask itself, it becomes tricky to
+ add headers to it. This function can be called instead of using a return
+ and you will get a response object which you can use to attach headers.
+
+ If view looked like this and you want to add a new header::
+
+ def index():
+ return render_template('index.html', foo=42)
+
+ You can now do something like this::
+
+ def index():
+ response = make_response(render_template('index.html', foo=42))
+ response.headers['X-Parachutes'] = 'parachutes are cool'
+ return response
+
+ This function accepts the very same arguments you can return from a
+ view function. This for example creates a response with a 404 error
+ code::
+
+ response = make_response(render_template('not_found.html', 404))
+
+ Internally this function does the following things:
+
+ - if no arguments are passed, it creates a new response argument
+ - if one argument is passed, :meth:`flask.Flask.make_response`
+ is invoked with it.
+ - if more than one argument is passed, the arguments are passed
+ to the :meth:`flask.Flask.make_response` function as tuple.
+
+ .. versionadded:: 0.6
+ """
+ if not args:
+ return current_app.response_class()
+ if len(args) == 1:
+ args = args[0]
+ return current_app.make_response(args)
+
+
def url_for(endpoint, **values):
"""Generates a URL to the given endpoint with the method provided.
The endpoint is relative to the active module if modules are in use.
View
18 tests/flask_tests.py
@@ -393,6 +393,24 @@ def from_tuple():
assert rv.status_code == 400
assert rv.mimetype == 'text/plain'
+ def test_make_response(self):
+ app = flask.Flask(__name__)
+ with app.test_request_context():
+ rv = flask.make_response()
+ assert rv.status_code == 200
+ assert rv.data == ''
+ assert rv.mimetype == 'text/html'
+
+ rv = flask.make_response('Awesome')
+ assert rv.status_code == 200
+ assert rv.data == 'Awesome'
+ assert rv.mimetype == 'text/html'
+
+ rv = flask.make_response('W00t', 404)
+ assert rv.status_code == 404
+ assert rv.data == 'W00t'
+ assert rv.mimetype == 'text/html'
+
def test_url_generation(self):
app = flask.Flask(__name__)
@app.route('/hello/<name>', methods=['POST'])
Please sign in to comment.
Something went wrong with that request. Please try again.