Generates a fully qualified URL for a named :app:`Pyramid` :term:`route configuration` based on the 'current route'. This function supplements :meth:`pyramid.request.Request.route_url`. It presents an easy way to generate a URL for the 'current route' (defin
(self, *elements, **kw)
| 685 | return self.static_url(path, **kw) |
| 686 | |
| 687 | def current_route_url(self, *elements, **kw): |
| 688 | """ |
| 689 | Generates a fully qualified URL for a named :app:`Pyramid` |
| 690 | :term:`route configuration` based on the 'current route'. |
| 691 | |
| 692 | This function supplements |
| 693 | :meth:`pyramid.request.Request.route_url`. It presents an easy way to |
| 694 | generate a URL for the 'current route' (defined as the route which |
| 695 | matched when the request was generated). |
| 696 | |
| 697 | The arguments to this method have the same meaning as those with the |
| 698 | same names passed to :meth:`pyramid.request.Request.route_url`. It |
| 699 | also understands an extra argument which ``route_url`` does not named |
| 700 | ``_route_name``. |
| 701 | |
| 702 | The route name used to generate a URL is taken from either the |
| 703 | ``_route_name`` keyword argument or the name of the route which is |
| 704 | currently associated with the request if ``_route_name`` was not |
| 705 | passed. Keys and values from the current request :term:`matchdict` |
| 706 | are combined with the ``kw`` arguments to form a set of defaults |
| 707 | named ``newkw``. Then ``request.route_url(route_name, *elements, |
| 708 | **newkw)`` is called, returning a URL. |
| 709 | |
| 710 | Examples follow. |
| 711 | |
| 712 | If the 'current route' has the route pattern ``/foo/{page}`` and the |
| 713 | current url path is ``/foo/1`` , the matchdict will be |
| 714 | ``{'page':'1'}``. The result of ``request.current_route_url()`` in |
| 715 | this situation will be ``/foo/1``. |
| 716 | |
| 717 | If the 'current route' has the route pattern ``/foo/{page}`` and the |
| 718 | current url path is ``/foo/1``, the matchdict will be |
| 719 | ``{'page':'1'}``. The result of |
| 720 | ``request.current_route_url(page='2')`` in this situation will be |
| 721 | ``/foo/2``. |
| 722 | |
| 723 | Usage of the ``_route_name`` keyword argument: if our routing table |
| 724 | defines routes ``/foo/{action}`` named 'foo' and |
| 725 | ``/foo/{action}/{page}`` named ``fooaction``, and the current url |
| 726 | pattern is ``/foo/view`` (which has matched the ``/foo/{action}`` |
| 727 | route), we may want to use the matchdict args to generate a URL to |
| 728 | the ``fooaction`` route. In this scenario, |
| 729 | ``request.current_route_url(_route_name='fooaction', page='5')`` |
| 730 | Will return string like: ``/foo/view/5``. |
| 731 | |
| 732 | """ |
| 733 | if '_route_name' in kw: |
| 734 | route_name = kw.pop('_route_name') |
| 735 | else: |
| 736 | route = getattr(self, 'matched_route', None) |
| 737 | route_name = getattr(route, 'name', None) |
| 738 | if route_name is None: |
| 739 | raise ValueError('Current request matches no route') |
| 740 | |
| 741 | if '_query' not in kw: |
| 742 | kw['_query'] = self.GET |
| 743 | |
| 744 | newkw = {} |
no test coverage detected