The redirect() function is just a shortcut for raising an HTTP exception 
with a status code of 303 (this is mentioned in the documentation 
<http://web2py.com/books/default/chapter/29/04/the-core#HTTP-and-redirect>). 
This allows a redirect to immediately abandon the current request and 
return an HTTP response.

In your first example, the validate() method does a redirect, so the code 
execution stops at that point, and you never get to the rest of the code in 
your function. In your second example, you have placed the redirect() call 
inside a "try" block, and then you don't do anything with the exception in 
the "except" block, so the HTTP exception never gets to finish executing 
(you simply print it but do not re-raise it). If you want to catch (and 
ignore) exceptions among a block of code that also includes a redirect, 
then you have to check for the HTTP exception and re-raise it:

try:
    ...
    redirect(...)
except Exception as e:
    if isinstance(e, HTTP):
        raise e
    else:
        print e

Anthony

On Sunday, October 26, 2014 11:09:24 AM UTC-4, Spokes wrote:
>
> Dabbled with it some more, and it seems that the problem exists not only 
> when using the super class to generate the form. I've tried a couple of 
> functions that generate the form within the controller. The first variant 
> is this (passing the 'next' directive to validate()):
>
> def create_form():
>     form =  SQLFORM.factory(db.t_mytable)
>
>     if form.validate(next = URL('my_controller', 'next_url')):
>         try:
>             print 'form.vars:', form.vars
>             record_id = MyTable.insert_record(form.vars)
>             session['new_record_id'] = record_id
>
>         except Exception as e:
>             print e
>             
>     return form
>
> Upon form submission, the app bypasses the code within the validate() 
> conditional, and redirects successfully to the url passed to validate().
>
> In the second variant, I'm trying to manually redirect after the code 
> within validate() is completed:
>
> def create_form():
>     form =  SQLFORM.factory(db.t_mytable)
>
>     if form.validate():
>
>         try:
>             print 'form.vars:', form.vars            
>             record_id = MyTable.insert_record(form.vars)
>             session['new_record_id'] = record_id
>
>             redirect(URL('my_controller', 'next_url')
>         except Exception as e:
>             print e
>             
>     return form
>
>
> Upon form submission, the code within the scope of the validate() 
> conditional is executed, a '303 SEE OTHER' exception is thrown, and the app 
> does not redirect to the function specified in URL().
>
> Any thoughts as to what could be happening, or ideas on how to debug it?
>

-- 
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