Published in 22:12 of 08/16/2012 by

Published in 22:12 of 08/16/2012


web2py - manage users and membership in the same form

As requested by user of Stack Overflow.

How to manage users and memberships at the same form

NOTE: You have to register the first admin user first, because to manage users and memberships we require to be admin

For the purpose of the example we are going to use the file controllers/ accessible at the url localhost:8000/YOURAPP/default

A grid to list your users

this is the list admins will see when hit http://..../default/list_users

The user list grid

1 - Put on the file

#@auth.requires_membership("admin") # uncomment to enable security 
def list_users():
    btn = lambda row: A("Edit", _href=URL('manage_user',
    db.auth_user.edit = Field.Virtual(btn)
    rows = db(db.auth_user).select()
    headers = ["ID", "Name", "Last Name", "Email", "Edit"]
    fields = ['id', 'first_name', 'last_name', "email", "edit"]
    table = TABLE(THEAD(TR(*[B(header) for header in headers])),
                  TBODY(*[TR(*[TD(row[field]) for field in fields]) \
                        for row in rows]))
    table["_class"] = "table table-striped table-bordered table-condensed"
    return dict(table=table)

With generic views will see this

2 - The edit links to manage_users

Now accessing http://..../default/list_users you are going to see the grid showing all users, now if you click on the edit link on grid it goes to manage_user function we defined on btn = lambda row: A("Edit", _href=URL('manage_user',

Create this two functions in the same controller

The user form
#@auth.requires_membership("admin") # uncomment to enable security 
def manage_user():
    user_id = request.args(0) or redirect(URL('list_users'))
    form = SQLFORM(db.auth_user, user_id).process()
    membership_panel = LOAD(request.controller,
    return dict(form=form,membership_panel=membership_panel)

On the above function we are going to create two objects form which is the form to edit the user object, also we create membership_panel which is an ajax panel to load the manage_membership inside it and ajax managed.

note: that this function takes user_id from request.args(0) then if it is not provided it redirects back to the list_users

The membership panel
#@auth.requires_membership("admin") # uncomment to enable security 
def manage_membership():
    user_id = request.args(0) or redirect(URL('list_users'))
    db.auth_membership.user_id.default = int(user_id)
    db.auth_membership.user_id.writable = False
    form = SQLFORM.grid(db.auth_membership.user_id == user_id,
    return form

note that on the manage_membership we are returning the form directly, so we can input it inside the ajax panel membership_panel

The manage_user view

3 - Create an html file in YOURAPP/views/default/manage_user.html

{{extend 'layout.html'}}
<h4> Edit The user </h4>
<h4> User membership </h4>

The end result

User Form

Add membership

Done, using web2py 2.0 (trunk)

  • O Natal Animal 2013 está no ar in Home · 23:24 of 12/10/2013
  • Desenvolvendo protótipos para startups com Python e web2py in web2py · 20:25 of 11/19/2013
  • Gravando logs de aplicativos web2py in web2py · 00:29 of 01/21/2013

  • comments powered by Disqus Go Top