accessing config variable in views

50 views
Skip to first unread message

Anil

unread,
Sep 2, 2012, 10:05:22 AM9/2/12
to pylons-...@googlegroups.com
In my 

def main(global_config, **settings)

I have the config = Configuration(...) variable.

How would I access this config variable from my views? Some people have suggested using request.registry.settings, but I don't see it in that dictionary in my 1.3.3 version.

Background:

I am using pyramid_ldap, and there is a config.ldap_set_login_query() that I would like to dynamically set (the ldap search base) depending on the type of request:

http://docs.pylonsproject.org/projects/pyramid_ldap/en/latest/

Also, if I wanted to do something before each and every request, what is the best way? What about just after the request is sent handled? Pylons used to have a __before__ and __after__, I don't think those are working here.

Thanks

Jonathan Vanasco

unread,
Sep 2, 2012, 1:31:47 PM9/2/12
to pylons-discuss

> How would I access this config variable from my views? Some people have
> suggested using request.registry.settings, but I don't see it in that
> dictionary in my 1.3.3 version.

request.registry.settings is the place to find app configuration
settings.

I just looked at the pyramid ldap docs, and the 'config' variable
there is a Configurator() instance. I could be wrong, but I don't
think that is the pyramid config registry. it might save things into
it, but I believe it is another instance.

the pyramid ldap docs also note a few things:

- there's a pretty decent API, and the only thing you should really
need to access in your views is 'get_ldap_connector()'.

- you could probably explore the object returned by
get_ldap_connector() to find the variables you want, but the package
seems to be designed for a single setup : 'All three of these methods
should be called once (and, ideally, only once) during the startup
phase of your Pyramid application'


> Also, if I wanted to do something before each and every request, what is
> the best way? What about just after the request is sent handled? Pylons
> used to have a __before__ and __after__, I don't think those are working
> here.

in most cases, you want to create an event subscriber :

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/api/events.html?highlight=events#pyramid.events


another alternative is to use a class-based approach to writing
views. this is the most pylons-esque approach, where you can use a
single base class that has an init function.

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/views.html#defining-a-view-callable-as-a-class

your code could look like this:

class _BaseView(object):
def __init__(self,request):
self.request = request
# do your shared stuff here

class LoginView(_BaseView):
def login_print(self):
pass
def login_submit(self):
pass

class AccountView(_BaseView):
def account_home(self):
pass
def account_password_change(self):
pass




Reply all
Reply to author
Forward
0 new messages