You wouldn't generally want to define your DAL connection or crud in your 
module -- instead, you would likely want to define them in your models and 
then pass them to your module function as arguments.
 
I think you would do something like:
 
def manage(request, db, crud, title, table)
 
and then in your controller function, call:
 
    manage.manage(request=request, db=db, crud=crud, title=T('blog'), 
table=db.blog)
 
An alternative is just to pass in all the globals in one argument via 
globals(). To see an example of that approach, check out the __init__ 
methods of the Auth and Crud classes in /gluon/tools.py.
 
You should probably also ask yourself whether you really need this function 
to be in a module rather than in a controller or model file. If it's only 
used in one controller, put it in that controller file. If it's used across 
multiple controllers, consider putting it in a model file (all model files 
are executed on every request). It's probably not worth moving it to a 
module unless you need it across multiple apps or your models are getting 
too bloated.
 
Anthony

On Saturday, April 9, 2011 1:22:32 PM UTC-4, 黄祥 wrote:

> i've tried to run what you suggested, but still have a problem: 
> On Modules:
>
>  #!/usr/bin/env python
> # coding: utf8
> from gluon.html import *
> from gluon.http import *
> from gluon.validators import *
> from gluon.sqlhtml import *
> # request, response, session, cache, T, db(s)
> # must be passed and cannot be imported!
>
> from gluon.tools import *
>
> db = DAL('sqlite://storage.sqlite')
> crud = Crud(globals(),db)
>
> def manage(title, table, request):
> form = crud.update(table, request.args(1))
> table.id.represent = lambda id: A('Edit:', id,  _href = URL(args = 
> (request.args(0), id)))
> search, rows = crud.search(table)
> return dict(title = title,  form = form, search = search, rows = rows)
>
> On Controller :
>
> # coding: utf8
>
>  exec('import applications.%s.modules.manage as manage' % 
> request.application)
> reload(manage)
>
>  def blog():
>     title = T('Blog')
>     table = db.blog
>     return manage.manage(title, table, request)
>
> i got an error :
>
> Traceback (most recent call last):
>
>
>   File "/home/sugizo/web2py/gluon/restricted.py", line 188, in restricted
>
>
>     exec ccode in environment
>
>
>   File 
> "/home/sugizo/web2py/applications/stevevanchristie/controllers/manage.py" 
> <http://127.0.0.1:8000/admin/default/edit/stevevanchristie/controllers/manage.py>,
>  line 4, in <module>
>
>
>     reload(manage)
>
>
>   File "applications/stevevanchristie/modules/manage.py", line 13, in <module>
>
>
>     crud = Crud(globals(),db)
>
>
>   File "/home/sugizo/web2py/gluon/tools.py", line 2671, in __init__
>
>
>     self.settings.delete_next = self.url()
>
>
>   File "/home/sugizo/web2py/gluon/tools.py", line 2659, in url
>
>
>     f=f, args=args, vars=vars)
>
>
>   File "/home/sugizo/web2py/gluon/html.py", line 218, in URL
>
>
>     raise SyntaxError, 'not enough information to build the url'
>
> SyntaxError: not enough information to build the url
>
>
> did anyone know how to fixed it?
>
> thank you very much
>

Reply via email to