Cleaning up web2py my controllers

爷,独闯天下 提交于 2019-12-05 05:54:26

You controller actions (i.e., the actions that appear in URLs) have to be functions defined in a controller file (i.e., you cannot move them to a module). However, if there are functions in your controller that are not actions, you may move those to a module. Assuming you will call those functions from a model or controller, you can simply pass your db, me, and now objects to those functions as arguments. Another option is to add them to the thread local current object, which can be accessed from modules. To do so:

In a model:

from globals import current
current.app.db = db
# etc.

In a module:

from globals import current

def func(*args):
    db=current.app.db
    # etc.

you can create python files in modules folder and import them just like how you import python libraries in your controllers. But you have to give the path to those files like

 from applications.myApp.modules.myModule import *

this is my solution for my wrappers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!