From 75461c1467bbb2d83ee787bb13f4a338a4b8ad3e Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 2 May 2010 19:10:44 +0200 Subject: [PATCH] Added _external support to url_for --- CHANGES | 1 + flask.py | 5 ++++- tests/flask_tests.py | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index ce7289da..c3489d27 100644 --- a/CHANGES +++ b/CHANGES @@ -14,6 +14,7 @@ Version 0.2 - :meth:`~flask.Flask.add_url_rule` can now also register a view function. - server listens on 127.0.0.1 by default now to fix issues with chrome. +- added external URL support. Version 0.1 ----------- diff --git a/flask.py b/flask.py index c1c3fdb7..a50b95be 100644 --- a/flask.py +++ b/flask.py @@ -152,8 +152,11 @@ def url_for(endpoint, **values): :param endpoint: the endpoint of the URL (name of the function) :param values: the variable arguments of the URL rule + :param _external: if set to `True`, an absolute URL is generated. """ - return _request_ctx_stack.top.url_adapter.build(endpoint, values) + external = values.pop('_external', False) + return _request_ctx_stack.top.url_adapter.build(endpoint, values, + force_external=external) def get_template_attribute(template_name, attribute): diff --git a/tests/flask_tests.py b/tests/flask_tests.py index 1759f8b9..f9369e36 100644 --- a/tests/flask_tests.py +++ b/tests/flask_tests.py @@ -227,6 +227,8 @@ class BasicFunctionalityTestCase(unittest.TestCase): pass with app.test_request_context(): assert flask.url_for('hello', name='test x') == '/hello/test%20x' + assert flask.url_for('hello', name='test x', _external=True) \ + == 'http://localhost/hello/test%20x' def test_custom_converters(self): from werkzeug.routing import BaseConverter