Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for _method to url_for()

  • Loading branch information...
commit f52e7a9dc944f425c2f2a77706bc2af98b23295c 1 parent 61a9519
@mitsuhiko authored
Showing with 33 additions and 2 deletions.
  1. +2 −0  CHANGES
  2. +5 −2 flask/helpers.py
  3. +26 −0 flask/testsuite/helpers.py
View
2  CHANGES
@@ -10,6 +10,8 @@ Relase date to be decided, codename to be chosen.
- The :func:`flask.url_for` function now can generate anchors to the
generated links.
+- The :func:`flask.url_for` function now can also explicitly generate
+ URL rules specific to a given HTTP method.
- Logger now only returns the debug log setting if it was not set
explicitly.
View
7 flask/helpers.py
@@ -187,12 +187,13 @@ def url_for(endpoint, **values):
For more information, head over to the :ref:`Quickstart <url-building>`.
.. versionadded:: 0.9
- The `_anchor` parameter was added.
+ The `_anchor` and `_method` parameters were added.
: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.
:param _anchor: if provided this is added as anchor to the URL.
+ :param _method: if provided this explicitly specifies an HTTP method.
"""
ctx = _request_ctx_stack.top
blueprint_name = request.blueprint
@@ -211,8 +212,10 @@ def url_for(endpoint, **values):
endpoint = endpoint[1:]
external = values.pop('_external', False)
anchor = values.pop('_anchor', None)
+ method = values.pop('_method', None)
ctx.app.inject_url_defaults(endpoint, values)
- rv = ctx.url_adapter.build(endpoint, values, force_external=external)
+ rv = ctx.url_adapter.build(endpoint, values, method=method,
+ force_external=external)
if anchor is not None:
rv += '#' + url_quote(anchor)
return rv
View
26 flask/testsuite/helpers.py
@@ -304,6 +304,32 @@ def index():
self.assert_equal(flask.url_for('index', _anchor='x y'),
'/#x%20y')
+ def test_url_with_method(self):
+ from flask.views import MethodView
+ app = flask.Flask(__name__)
+ class MyView(MethodView):
+ def get(self, id=None):
+ if id is None:
+ return 'List'
+ return 'Get %d' % id
+ def post(self):
+ return 'Create'
+ myview = MyView.as_view('myview')
+ app.add_url_rule('/myview/', methods=['GET'],
+ view_func=myview)
+ app.add_url_rule('/myview/<int:id>', methods=['GET'],
+ view_func=myview)
+ app.add_url_rule('/myview/create', methods=['POST'],
+ view_func=myview)
+
+ with app.test_request_context():
+ self.assert_equal(flask.url_for('myview', _method='GET'),
+ '/myview/')
+ self.assert_equal(flask.url_for('myview', id=42, _method='GET'),
+ '/myview/42')
+ self.assert_equal(flask.url_for('myview', _method='POST'),
+ '/myview/create')
+
def suite():
suite = unittest.TestSuite()
Please sign in to comment.
Something went wrong with that request. Please try again.