开发者

Some doubts on implementing custom error pages in Web2Py

I'm trying to implement a decorator for custom error pages in web2py as per one of the haiti Todos. 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 from a controller say (or.py)-

exec('from applications.%s.modules.onerror import开发者_如何学Go 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. Is /modules directory a good place to keep such a reusable component?

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

  3. 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.


Solved through the web2py users mailing list.

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 in web2py as well. 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 realized 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 normal python modules. You have to import them to use them.

  1. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜