Help with request.args() and SQLFORM.grid(). Error 404

23 views
Skip to first unread message

Juan Gutiérrez

unread,
Jan 15, 2020, 1:04:00 AM1/15/20
to web2py-users
This sounds crazy but I'm getting an error 404 when using in the same function this lines together:

test = db.test(request.args(0,cast=int, otherwise=URL('index'))) or redirect(URL('index'))
grid = SQLFORM.grid(db(db.test.id == 2), deletable=False, editable=True)

When I comment either one of the lines, the function returns the correct values for test and grid. When put together, it doesn't work and I get redirected or a 404 error.

the complete escenario is:
Request:

Model:
db.define_table('test',
                Field('nombre', 'string', unique=True, label='Nombre'),
                Field('gerencia', label='Gerencia'),
                Field('equipo', label='Equipo'),
                Field('vigencia', 'date', label='Vigencia'),
                format = '%(nombre)s'
                )

Controler:
@auth.requires_login()
def testArg():
    test = db.test(request.args(0,cast=int, otherwise=URL('index'))) or redirect(URL('index'))
    grid = SQLFORM.grid(db(db.test.id == 2))#, deletable=False, editable=True)
    return locals()

View:
generic.html

Am I missing something really obvious?

David.

Juan Gutiérrez

unread,
Jan 15, 2020, 6:37:41 AM1/15/20
to web2py-users
Ok... So I found the solution and it was a TOTAL noob mistake. Turns out that SQLFORM.grid() messes up with the requests arguments and in the initial examples of the book this is never mentioned.

In Chapter 7 of the book (english 6th edition) I found the answer: (http://web2py.com/books/default/chapter/29/07/forms-and-validators#Using-requests-args-safely)

In order for the grid to work properly, I had to give it this parameter: args=request.args[:1] that way the Grid knows to preserve the first argument for the request.

The code in the controller ended up working like this:
@auth.requires_login()
def testArg():
    test = db.test(request.args(0,cast=int, otherwise=URL('index'))) or redirect(URL('index'))
    grid = SQLFORM.grid(db(db.test.id == 2), args=request.args[:1], deletable=False, editable=True)
    return locals()
Reply all
Reply to author
Forward
0 new messages