def url_for(*args, **kw):
        self, target, args = args[0], args[1], list(args[2:])
        query = kw.pop('_query', None)
        relative = kw.pop('_relative', False)
        url = build_url(self._context, target, args, kw)
        if query:
            if isinstance(query, dict):
                query = sorted(query.items())
            query_part = urllib.urlencode(query)
            query_sep = '&' if '?' in url else '?'
            url = url + query_sep + query_part
        if relative:
            return url
        else:
            return urlparse.urljoin(self.application_uri, url)