6. Controller
- Even though we have a
CRUD
form created for us in the database administration tool, we wouldn't want to expose the entire appadmin to our users. So we need to create a controller and a view. - All Web2py applications have two controllers supplied as part of the scaffolding -
appadmin.py
and default.py
. You typically don't want to touch appadmin. You do want to place your controller definitions in default.py
. The code below is for the manage_aliases
function found in default.py
. - Line 23 is a decorator to the
manage_aliases
function which requires the user to be a member of the managers
security group. - Line 24 is the function definition.
- Line 25 sets
response.title
to "Manage Aliases". The T()
function will be discussed later and is used for internationalization of strings. - Line 26
grid
is a type of SQLFORM.grid()
. SQLFORM.grid
is a high level object with complex CRUD
controls. The first and only required argument is a table name. In this instant the table is db.the_aliaser
. - Line 27 The
fields
argument allows you to specify which fields will appear in the grid and in what order. The default is to show all the fields from the table. - Line 28 Sets the
maxtextlengths
of the alias_url
column to 200 characters. - Line 30 return all the variables from this function to the view.
##### Copy the code below and paste into default.py #####
@auth.requires_membership('manager')
def manage_aliases():
response.title = T("Manage Aliases")
grid = SQLFORM.grid(db.the_aliaser,
fields=(db.the_aliaser.name, db.the_aliaser.alias_url),
maxtextlengths= {'the_aliaser.alias_url' : 200})
return locals()
- More on function decorators
[ Previous ] [ Next ]