Sank
Sank

Reputation: 131

How to use route_url method in ApplicationCreated event

I subscribed on ApplicationCreated and I need to get same urls in my web app.
In views I can to use route_url method to get urls.
But how to get url in ApplicationCreated event?

For example in view I can use this code:

from pyramid.view import view_config

@view_config(route_name="home")
def home(request):
    print request.route_url('home')

Result in console:

http://example.com/

How I can use same code in this case:

from pyramid.events import ApplicationCreated, subscriber

@subscriber(ApplicationCreated)
def app_start(event):
    print ????.route_url('home') # how to get access to route_url

Upvotes: 0

Views: 73

Answers (1)

uralbash
uralbash

Reputation: 3618

I think it's no way. You are trying to use request-required method in event where no request yet. See sourcecode https://github.com/Pylons/pyramid/blob/master/pyramid/config/init.py#L995

W/o request you can get just route object:

home = app.routes_mapper.get_route('home')
print(home.path)

Example with custom generate URL:

delete = app.routes_mapper.get_route('pyramid_sacrud_delete')
print(delete.path)  # 'admin/{table}/delete/*pk'
delete.generate({'table': 'foo', 'pk': ['id',2,'id2',3]})  # '/admin/foo/delete/id/2/id2/3'

Your example:

from pyramid.events import ApplicationCreated, subscriber

@subscriber(ApplicationCreated)
def app_start(event):
    print event['app'].app.routes_mapper.get_route('home')

Upvotes: 1

Related Questions