If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

originally posted on my coding blog: http://brunorocha.org/python/web2py/web2py-manage-users-and-membership-in-the-same-form.html


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/default.py 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 default.py file

#@auth.requires_membership("admin") # uncomment to enable security 
def list_users():
    btn = lambda row: A("Edit", _href=URL('manage_user', args=row.auth_user.id))
    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', args=row.auth_user.id))

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 thelist_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,
                       user_signature=False)  # change to True in production
    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)


Related slices

Comments (8)

  • Login to post

  • 0
    juli 1 month ago

    I need to add "Delete Button" on List User.

    How I can do this?

  • 0
    sif-baksh-10957 1 year ago

    Many thanks  Bruno Rocha, this is great.

  • 0
    web2pyslices 2 years ago

    Updated the code. added .process() in the form and also user_signature=False to the grid.



  • 0
    marc-garcia-10770 2 years ago

    @jkumar There are code missing, you have to create also a view as "default/list_users" (Or whatever the function name that creates the table) and in the view you have to type this:


    {{extend 'layout.html'}}



    replies (1)
    • web2pyslices 2 years ago

      Yes, that was a change in web2py.

      You can create the new view in 'default/list_users.html' or optionally you can just set the generic patterns to *

      response.generic_patterns = ['*'] in db.py

  • 0
    jkumar 2 years ago

    I am trying the same with auth_group and "my_custom_table".

    I am not getting the drop down to map the tables.


    Here is my membership table.

          Field("devices_config_id", "integer", db.devices_config),
          Field("auth_group_id", "integer", db.auth_group))
show more comments

Hosting graciously provided by:
Python Anywhere