Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 661995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:19:46+00:00 2026-05-13T23:19:46+00:00

I’m trying to implement a decorator for custom error pages in web2py as per

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:19:46+00:00Added an answer on May 13, 2026 at 11:19 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.