Problem trying to get a value from a rows object

284 views
Skip to first unread message

Christian Espinoza

unread,
Aug 21, 2012, 6:36:00 PM8/21/12
to web...@googlegroups.com
Hello, I'm trying to edit the group membership for a user, I have in my controller:

def change_membership():

    if request.vars.id:

        row = db(db.auth_membership.user_id == request.vars.id).select()
        id = row[0].id  
        form = SQLFORM(db.auth_membership,
                                   id,
                                   fields=['group_id'],
                                   _action=URL()
                                   )
    if form.process().accepted:
        ...redirect back to user list

    if form.errors:
        response.flash = 'form has errors'
   
    return dict(form=form)
 

But It doesn't work, I get a :

<type 'exceptions.IndexError'> list index out of range


I know that only get one row, but I don't understand why its seems empty..

Thanks in advance
Christian

Bruno Rocha

unread,
Aug 21, 2012, 6:58:47 PM8/21/12
to web...@googlegroups.com
You are getting an empty rows object


def
 change_membership():

    if request.vars.id:

        row = db(db.auth_membership.user_id == request.vars.id).select()
        if not row:
            redirect(.........)
        id = row[0].id  
        form = SQLFORM(db.auth_membership,
                                   id,
                                   fields=['group_id'],
                                   _action=URL()
                                   )
    if form.process().accepted:
        ...redirect back to user list

    if form.errors:
        response.flash = 'form has errors'
    
    return dict(form=form)

also can be done with

 row = db(db.auth_membership.user_id == request.vars.id).select() or redirect(.....)




Christian Espinoza

unread,
Aug 21, 2012, 7:09:27 PM8/21/12
to web...@googlegroups.com
Hi Bruno, this error appear after I submit the changes on the form, 

I can get the form without problems for a user, but the problem appears after submit any change.

Christian

Anthony

unread,
Aug 21, 2012, 7:11:35 PM8/21/12
to web...@googlegroups.com
Also, note the .select().first() returns the first row or None if there are no results, so you don't have to test whether there are any records before selecting the first row.

Anthony

Christian Espinoza

unread,
Aug 21, 2012, 7:16:18 PM8/21/12
to web...@googlegroups.com, abas...@gmail.com
Hi Anthony,  I was try with .select().first(), but I get a error in the same way...

The Error appears after I submit the form with the changes over the field, the form for a id user works, the problem is when I submit it.

Thanks.
Christian

2012/8/21 Anthony <abas...@gmail.com>

--
 
 
 

Anthony

unread,
Aug 21, 2012, 7:19:04 PM8/21/12
to web...@googlegroups.com, abas...@gmail.com
How is request.vars.id being set -- in the query string? What is its value when the form is created, and what is the value when submitted?

Christian Espinoza

unread,
Aug 22, 2012, 9:04:24 AM8/22/12
to web...@googlegroups.com, abas...@gmail.com
I'm sending the id var from a user list view on this way:

edit_user.html:

<a href="{{=URL('admin', 'change_membership', args=(), vars=dict(id=id, type='user'))}}" class="nyroModal">Change Membership</a>

edit_user.py:

def edit_user():


etc....

return dict(form=form, id=request.vars.id)

the id var come from another view when I was iterating over a query to get auth_user.id:

but on every step, I can trace the id value, I traced it with web2py debugger...

Maybe I'm not clear with web2py Post procedure, because all is right until I submit the last form, it come with the right id's

If I try with a literal var number it's works without problems....

Thanks in advance.
Christian

2012/8/21 Anthony <abas...@gmail.com>

--
 
 
 

Anthony

unread,
Aug 22, 2012, 9:31:34 AM8/22/12
to web...@googlegroups.com, abas...@gmail.com
Perhaps you can pack and attach a minimal app that reproduces the problem (or at least show more code). It's still not clear where that id comes from or what's happening to it.

Anthony

Christian Espinoza

unread,
Aug 22, 2012, 9:51:38 AM8/22/12
to web...@googlegroups.com, abas...@gmail.com
When I submit the form, on request.vars.id exist this value: ['2', '2']

this is the problem, but I dont know why, Im sending the var from the controller to it self with the 2  value only..

'id' is a reserved function??

Christian.

2012/8/21 Anthony <abas...@gmail.com>

--
 
 
 

Anthony

unread,
Aug 22, 2012, 9:54:39 AM8/22/12
to web...@googlegroups.com, abas...@gmail.com
It's really hard to say without seeing the code that generates the id. Somehow your id is a list instead of a single value.

Anthony

Christian Espinoza

unread,
Aug 22, 2012, 10:11:31 AM8/22/12
to web...@googlegroups.com, abas...@gmail.com
Hello Antony,

Before submit the form, id -> 2 , for example.

After submit the form id ->  ['2', '2']

maybe id is a reserved word for Web2py, when I did trace it on the debugger I saw that it change from a int number to a list,
only in the process of submit a form, without a user interaction.


Christian.

2012/8/22 Anthony <abas...@gmail.com>

--
 
 
 

Massimo Di Pierro

unread,
Aug 22, 2012, 10:14:12 AM8/22/12
to web...@googlegroups.com, abas...@gmail.com
You have two id fields. One in request.get_vars.id (from the url) and one in request.post_vars.id (from the form submission).

Anthony

unread,
Aug 22, 2012, 10:42:02 AM8/22/12
to web...@googlegroups.com, abas...@gmail.com
Yes. Note, web2py stores GET variables in request.get_vars and POST variables in request.post_vars. It stores both GET and POST vars in request.vars. If both get_vars and post_vars have variables with the same name, it puts their values in a list within request.vars. Just change your code to use request.get_vars.id instead of request.vars.id.

Anthony

Christian Espinoza

unread,
Aug 22, 2012, 11:20:08 AM8/22/12
to web...@googlegroups.com, abas...@gmail.com
Excellent! 

Thanks a lot Anthony and Massimo.

It´s save my day, now all is working using request.get_vars.id instead of request.vars.id


Christian.

2012/8/22 Anthony <abas...@gmail.com>

--
 
 
 

Reply all
Reply to author
Forward
0 new messages