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

> On May 12, 2021, at 15:01, QLands Software <qlands.softw...@gmail.com> wrote:
> 
> 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/";
> 
> -- 
> 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-discuss+unsubscr...@googlegroups.com 
> <mailto:pylons-discuss+unsubscr...@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
>  
> <https://groups.google.com/d/msgid/pylons-discuss/40b8550f-f57a-4152-a744-0bb4304251een%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
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-discuss+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/BE8DF89B-C546-4092-A2D7-C2A4C65DF7C6%40gmail.com.

Reply via email to