ok small corretion, you need it as staticmethod (or need a instance before
calling)

change to:

class Search(object):
   @*staticmethod*
   def show(db):
       ....


*Explanations about imports:*

from gluon impot *

The above is to have access to web2py helpers and other gluon modules, if
you dont do that you cant use TAG, DIV, etc inside the module scope.

request = current.request

The above is for access the request object from the module scope, without
that the module does not know about the request, reponse and session
objects.


On Mon, Aug 22, 2011 at 5:28 AM, Jarrod Cugley <jcug...@gmail.com> wrote:

> I've run into a problem, why won't this work:
>
> # controllers/default.py
> def search():
>     return dict(showsearch=objects.Search.show(db))
> # controllers/default.py
>
> # modules/objects.py
> from gluon import *
> request = current.request
>
> class Search(object):
>    def show(self, db):
>         search =
> db(db.listing.title==request.args(0)).select(db.listing.ALL)
>        items = []
>        for person in search:
>            items.append(DIV(A(person.first_name, _href=URL('listing',
> args=person.id))))
>
>        return TAG[''](*items)
> # modules/objects.py
>
> I'm so confused as to why that won't work, it's returning the error:
> AttributeError: type object 'Search' has no attribute 'show'
>
> On Aug 22, 5:57 pm, Jarrod Cugley <jcug...@gmail.com> wrote:
> > 1 more thing, could you explain these lines please:
> >
> > from gluon import *
> > request = current.request
> >
> > On Aug 22, 4:46 pm, Bruno Rocha <rochacbr...@gmail.com> wrote:
> >
> >
> >
> >
> >
> >
> >
> > > I guess it is not wrong, but I do not recommend too much code in
> > > controllers, controllers should be for decide the app flow.
> >
> > > I reccomend you to create a module in /modules
> >
> > > # modules/myobjects.py
> > > from gluon import *
> > > request = current.request
> > > class Myobjects(object):
> > >     def showsearch(self, db):
> > >         search
> > > = db(db.listing.title==request.args(0)).select(db.listing.ALL)
> > >         items = []
> > >         for person in search:
> > >             items.append(DIV(A(person.first_name,
> _href=URL('listing',args=
> > > person.id))))
> >
> > >        return TAG[''](*items)
> > > # modules/myobjects.py
> >
> > > then in controller you do:
> >
> > > # controllers/default.py
> >
> > > def search():
> > >     from myobjects import Myobjects
> > >     return dict(showsearch=Myobjects.showsearch(db))
> >
> > > # controllers/deafault.py
> >
> > > Note that you need to pass 'db' instance to the module
> >
> > > I think the controller code looks much better in this way, needs web2py
> > > 1.97+
> >
> > > On Mon, Aug 22, 2011 at 2:10 AM, Jarrod Cugley <jcug...@gmail.com>
> wrote:
> > > > Is there anything wrong with doing this inside default.py
> controller?:
> >
> > > > def search():
> > > >    def showsearch():
> > > >        search =
> > > > db(db.listing.title==request.args(0)).select(db.listing.ALL)
> > > >        items = []
> > > >        for person in search:
> > > >            items.append(DIV(A(person.first_name, _href=URL('listing',
> > > > args=person.id))))
> >
> > > >        return TAG[''](*items)
> > > >    return dict(showsearch=showsearch())
> >
> > > > That is, nesting functions inside functions, I'm not getting an
> error,
> > > > it's working as intended but is this a horrible practice to get into?
> > > > Is there an ideal way?
> >
> > > --
> >
> > > --
> > > Bruno Rocha
> > > [ About me:http://zerp.ly/rochacbruno]
> > > [ Aprenda a programar:http://CursoDePython.com.br]
> > > [ O seu aliado nos cuidados com os animais:http://AnimalSystem.com.br]
> > > [ Consultoria em desenvolvimento web:http://www.blouweb.com]
>



-- 



--
Bruno Rocha
[ About me: http://zerp.ly/rochacbruno ]
[ Aprenda a programar: http://CursoDePython.com.br ]
[ O seu aliado nos cuidados com os animais: http://AnimalSystem.com.br ]
[ Consultoria em desenvolvimento web: http://www.blouweb.com ]

Reply via email to