Use route_url just after the routes are added and without request

21 views
Skip to first unread message

QLands Software

unread,
May 12, 2021, 4:01:30 PM5/12/21
to pylons-discuss
I have a Pyramid application that adds routes using:

    config.add_route("home", "/")
            config.add_view(
                homeView,
                route_name="home",
                "home.jinja2",
            )

The application uses PCA https://github.com/PyUtilib/pyutilib therefore plug-ins can add routes to the main application.

After the main app and the plugins add the routes I would like to get the URL of a route for example "home". In views I use "request.route_url()" but I need to get the same during the initialization of the App, meaning just after

    config.make_wsgi_app()

I tried to use the registry introspection:

     config.add_route("home", "/")
            config.add_view(
                homeView,
                route_name="home",
                "home.jinja2",
            )
     config.make_wsgi_app()
     introspector = config.registry.introspector
     route_intr = introspector.get('routes', "home")
     print(route_intr)

But I get a route object with path and name but not with host, port, etc. I also tried:

    from pyramid.interfaces import IRoutesMapper
    mapper = config.registry.getUtility(IRoutesMapper)
    route = mapper.get_route("home")

I saw some code in Flask one can do something like this:

    from flask.helpers import url_for

    app.register_blueprint(views)
    url = url_for("home")

Is there something like that for Pyramid? Basically, I need to get for the route "home" the same result as if I would do request.route_url("home") which is "http://localhost:5900/"

Michael Merickel

unread,
May 12, 2021, 4:26:48 PM5/12/21
to pylons-...@googlegroups.com
At that config-time in the application there is no active request and no server running. In Pyramid, all url-generation APIs rely on creating a url "relative to the wsgi environ" or "relative to the current request". This keeps the app itself easy to mount into complex existing url hierarchies. This is done using properties of the request (script_name, port, host, scheme).

The way to construct a full URL is to create a dummy request object:

from pyramid.request import Request

request = Request.blank(base_url='http://localhost:5900')
request.registry = config.registry
assert request.route_url('home') == 'http://localhost:5900/'

The Request.blank() api constructs a wsgi environ with enough information for Pyramid to generate a url relative to it similar to what it would do if a request was coming from an actual wsgi server.

- Michael

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/40b8550f-f57a-4152-a744-0bb4304251een%40googlegroups.com.

QLands Software

unread,
May 13, 2021, 7:43:07 AM5/13/21
to pylons-discuss
Hi Michael,

Thanks for the answer. It works. 

Now I have a follow-up question: How can at that moment get the host and port used? Instead of a fixed string. I have that in the config in a section called [server:main] but is not available at config.registry.settings

Cheers

QLands Software

unread,
May 13, 2021, 8:09:39 AM5/13/21
to pylons-discuss
Reply all
Reply to author
Forward
0 new messages