Custom 404 pages

98 views
Skip to first unread message

Abhishek Mishra

unread,
Mar 5, 2010, 10:48:03 PM3/5/10
to web2py-users
Hi I'm trying to implement a decorator for custom error pages in
web2py into sahanapy.
Ref - http://web2py.com/AlterEgo/default/show/75

I'm trying to keep it as a module in /modules directory so that I can
import it into the controllers and place the decorator appropriately.

I have kept error handling decorator as
/modules/onerror.py

and am importing it like this -
exec('from applications.%s.modules.onerror import onerror as onerror'
%
request.application)

HTTP object wasn't available in onerror.py so i did a -
from gluon.http import *

But then I readlized that the request object is also not available to
the decorator as in the line -
filename=os.path.join(request.folder,'views/errors/error
%i.html'%status)

I have some doubts -

1. Are the modules in the /modules directory automatically loaded as a
new web2py instance is created? If so how can I access them?

2. Is there a way I can pass the request object to this decorator from
my controller? Hints are welcome.

My current onerror.py - http://paste.pocoo.org/show/186165/
The way I'm trying to use in a controller - http://paste.pocoo.org/show/186167/

Let me know if I'm doing it in an unobvious way.

Thanks,
Abhishek

mdipierro

unread,
Mar 6, 2010, 9:58:18 AM3/6/10
to web2py-users
On Mar 5, 9:48 pm, Abhishek Mishra <ideam...@gmail.com> wrote:
> Hi I'm trying to implement a decorator for custom error pages in
> web2py into sahanapy.
> Ref -http://web2py.com/AlterEgo/default/show/75

>
> I'm trying to keep it as a module in /modules directory so that I can
> import it into the controllers and place the decorator appropriately.
>
> I have kept error handling decorator as
> /modules/onerror.py
>
> and am importing it like this -
> exec('from applications.%s.modules.onerror import onerror as onerror'
>                                                         %
> request.application)

you can also use:

onerror = load_import('onerror').onerror

> HTTP object wasn't available in onerror.py so i did a -
> from gluon.http import *

python modules are normal python modules also in web2py. They only see
python keywords unless you import them. web2py has 5 special objects
(request, response, session, cache, T) and your app has others (db,
auth, crud). Because they are instantiated at every request, they
cannot import by the module, they have to be passed explicitly to the
functions in the module. Mind that while this is awkward this is what
other frameworks ask to do all the time for all functions. web2py
saves you this for models and controllers.

> But then I readlized that the request object is also not available to
> the decorator as in the line -
> filename=os.path.join(request.folder,'views/errors/error
> %i.html'%status)
>
> I have some doubts -
>
> 1. Are the modules in the /modules directory automatically loaded as a
> new web2py instance is created? If so how can I access them?

No. They are norml python modules. You have to import them to use
them.

> 2. Is there a way I can pass the request object to this decorator from
> my controller? Hints are welcome.

You need a meta decorator

def meta_decoration(something):
def actual_decorator(f,something=something):
do_whatever_you_want()
return f()
return decorator

@meta_decorator(request)
def function_to_be_decorated(): return dict()

Anyway you do not need this decorator. Look into routes.examples.py
for routes_onerror. There is a simple way to do what you want.

> My current onerror.py -http://paste.pocoo.org/show/186165/
> The way I'm trying to use in a controller -http://paste.pocoo.org/show/186167/

Abhishek Mishra

unread,
Mar 10, 2010, 1:10:04 PM3/10/10
to web2py-users
Thanks mdipierro,

I was of the belief that routes.py would only let me write global
error redirects and not application specific ones. But taking a look
at the examples, I could figure out what I wanted. More specifically
something like 'sahana/401'->'/sahana/default/user/login' , etc helps
out for app specific error pages.
Yup using the decorators would invite me to the less maintainable task
of adding it above all the controllers defs.

Thanks!

On Mar 6, 7:58 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> On Mar 5, 9:48 pm, Abhishek Mishra <ideam...@gmail.com> wrote:
>
> > Hi I'm trying to implement a decorator forcustomerrorpagesin
> > web2py into sahanapy.
> > Ref -http://web2py.com/AlterEgo/default/show/75
>
> > I'm trying to keep it as a module in /modules directory so that I can
> > import it into the controllers and place the decorator appropriately.
>

> > I have kepterrorhandling decorator as

Reply all
Reply to author
Forward
0 new messages