Antony, thank you very much, I got the 1st and the 3rd ones. But about the
2nd one it's tricky. Without going into details of why I need it, could you
please just glance at the following code that works 100%:

def test():
    id = 0
    try:
        id = request.args(0, cast=int)
    except:
        id = 0
        pass
       """ another 100 lines of code is cut off here
    grid = SQLFORM.grid(db.test_table, user_signature=False)
    return locals()

I am calling it, for example, like this:    app/default/test/1
How I use this id field it doesn't really matter - I've simply extracted
the essence of what's going on, leaving another 100 lines of code out.

This simply displays an old fashioned grid. Nothing fancy. But now consider
just exactly same code, with 2 extra lines (if (id==0): redirect(home) :

def test():
    id = 0
    try:
        id = request.args(0, cast=int)
    except:
        id = 0
        pass
    if (id==0):
        redirect('home')
    grid = SQLFORM.grid(db.test_table, user_signature=False)
    return locals()

Now, this looks exactly like the previous one - but this grid is NOT
functional. Neither create nor delete work! The buttons are there, but the
create and delete functions do NOT perform.

Please note that I always specify the parameters - app/default/test/1   or
app/default/test/100 - it's never called without a parameter. So logically,
unless I am missing something, if (id==0) should never be the case - right?

What I was surprised to find out was that WHEN I, for example, click delete
button on the grid - the page quietly reloads and this time it's called
without my parameter (without be doing anything and without me being aware
of this), thus (id==0) condition is true, executes appropriate code (which
was never meant to be executed if everything goes right), and this is how
it's somehow messes up delete and create.

I hope I've explained better this time...

On Thu, Dec 27, 2018 at 6:35 PM Anthony <[email protected]> wrote:

> On Thursday, December 27, 2018 at 4:05:40 PM UTC-5, Vlad wrote:
>>
>> Here is a test function in my controller:
>>
>> def test():
>>     id = 0
>>     try:
>>         id = request.args(0, cast=int)
>>         session.flash = "Here is the id: " + str(id)
>>     except:
>>         session.flash = "ID is not valid!"
>>         pass
>>     if id==0:
>>         redirect(URL('home'))
>>     query = db.test_table.id == id
>>     grid = SQLFORM.grid(query, db.test_table, user_signature=False)
>>     return locals()
>>
>
> Regarding the flash message, you should be setting response.flash, not
> session.flash (the latter goes in the session and will not be displayed
> until the *next *request, as you have observed -- it is typically used
> just before a redirect).
>
> What are you attempting to do with this function? Why are you even using
> the grid if you are only intending to display a single record?
>
> Note, your code does nothing in case the request.args(0) is not a valid
> integer -- in that case, your function still returns a grid (though
> presumably using a query you don't want). You might instead want to do a
> redirect in that case.
>
> Also, if your function is going to make use of some of the URL args, you
> must indicate this to the grid by passing in the URL args it is supposed to
> use (via the "args" argument).
>
> Finally, actions other than reading data require either a logged in user
> or user_signature=False.
>
> Anthony
>
> --
> Resources:
> - http://web2py.com
> - http://web2py.com/book (Documentation)
> - http://github.com/web2py/web2py (Source code)
> - https://code.google.com/p/web2py/issues/list (Report Issues)
> ---
> You received this message because you are subscribed to the Google Groups
> "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to