0

I am new to web2py and python both. I am writing a sample blog app in this framework. I want to split the business logic that gets called in each controller method to it's own module, and found this example helpful:

As you can see, you need to import objects in modules or set them through globals.current. The controller can refer to "db" and "request" instances (for example) without any import. What kind of coding mechanism makes it possible in controller but not elesewhere?

Community
  • 1
  • 1
SkP
  • 95
  • 2
  • 7

2 Answers2

0

The web2py framework does a lot of behind the scenes work to make all that stuff available. For example, when you go to a URL like host/app/controller, that controller is called by web2py (starting with something in web2py.py) that handles importing web2py modules, providing request/response objects, etc.

Things placed in modules, however, are intended to be standalone Python code, not necessarily specific to web2py.

Isaac Dontje Lindell
  • 3,246
  • 6
  • 24
  • 35
  • How does it do that - i.e the coding mechanism? (this might be more python related, or if you can point to a specific line of code in web2py source, that might also help) I ask because the controller functions don't take an argument. The objects "request", "db" ..etc are just magically present – SkP Jul 22 '13 at 17:03
0

Found the answer: Looks like how web2py works is by compiling the python code for the controllers and models and views on the fly. It runs them in it's special 'environment' Related snippets of code are:

SkP
  • 95
  • 2
  • 7