web2py - Where do I import a function if I want to use it in a view? -


i define variable in view so:

{{import datetime}} {{top10 = db( db.table.date > datetime.datetime.now() ).select()[:10]}} 

and go on use variable.

as in view want extend i'd rather not define variable in controller , pass through, why implemented did.

i don't i'm importing in view.

is there alternative (a) importing in view (b) defining variable in view? if practice i'm happy keep it.

thank you!

anything add environment in model file available in view, include import statement in model file. however, don't think there reason not import in view, may make more sense given module being used there (and not in model file).

if goal minimize logic in view, better approach might move both lines model file (you can use conditional models if need top10 variable particular controllers/functions).

also, in case, don't need datetime module, can use request.now in place of datetime.datetime.now() (the difference request.now have been calculated few milliseconds earlier, value populated framework @ start of request).

also, can make database query more efficient using limitby limit select first 10 records, rather selecting matching records , using python extract first 10:

top10 = db(db.table.date > request.now).select(limitby=(0, 10)) 

finally, if top 10 records don't change frequently, might consider caching select.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -