Quick Table Management Snippet
Just a little snippet that I find myself using again and again, to quickly create navigation to List, update and create tables:
In your controller:
def mylink(field, type, ref):
"""
Returns a URL to link to in crud.selects.
"""
return URL(r=request, args=[field])
def table_name():
"""
Maintain all the table_name records in the application
Manage, list, create and update projects.
"""
# SELECT if no args
if len(request.args)<1:
return dict(form=crud.select(db.table_name,linkto=mylink))
# If 1 arg and it is 'create' then CREATE
elif len(request.args)==1 and request.args[0]=='create':
return dict(form=crud.create(db.table_name,next=URL(request.application,request.controller,'table_name')))
# If 1 arg and its integer, EDIT it.
elif len(request.args)==1:
try:
i=int(request.args[0])
except:
redirect(URL(r=request,f='index'))
return dict(form=crud.update(db.table_name,i,next=URL(request.application,request.controller,'table_name')))
else:
redirect(URL(r=request,f='index'))
This leaves the following URLs for your table_name actions:
- /appname/controller/table_name will display a SELECT of the table.
- /appname/controller/table_name/create will CREATE a new record for the table.
- /appname/controller/table_name/edit/ID will EDIT table_name.id == ID
Closing up
I really hope that this little snippet saves some of your time for quickly prototyping apps.
Benigno Calvo Adiego, author of the present article, is co-founder of AlbenDas and executive director of the IT division at AlbenDas. Has a 11 year career as system analyst, project manager and IT Director managing external outsourced companies in various industrial and leisure business. Currently uses Web2Py as a quick integration tool to quickly adapt to constant changing requirements.
Comments (0)