Would appreciate a clarification on using redirect() from a component

48 views
Skip to first unread message

Spokes

unread,
Nov 13, 2014, 1:51:08 PM11/13/14
to web...@googlegroups.com
I have a view which contains a form that's within a LOAD component. The basic code for the component is something like this:

def function1():
   
try:
       
...
       form
= SQLFORM(db.my_table, row.id)
       
...
       
if form.process().accepted:
           
...
           redirect
(URL('controller1', 'function2'), client_side = True)    

   
except Exception as e:
       
if isinstance(e, HTTP):
               
raise e

If using the code above, after the form is processed, it will redirect to ".../[my app's name]/controller1/function2.load". If the URL() portion of the above code is changed to URL('controller1', 'function2.html'), the redirect seemingly behaves more or less as expected (i.e. loads the desired html view, 'function2', normally, albeit with '.html' at the end of the url). Why is it necessary to append ".html" to the function name to prevent a redirect to a non-existent LOAD component?

Leonel Câmara

unread,
Nov 13, 2014, 2:07:08 PM11/13/14
to web...@googlegroups.com
Because when you don't specify, the URL function uses the extension of the current request. More specifically URL does this:

if extension is None and r.extension != 'html':
            extension
= r.extension


Extension is None by default if you don't specify. However later URL also does this:

        if '.' in function:
           
function, extension = function.rsplit('.', 1)


So another way to declare the extension you want (instead of passing it to URL with extension=) is to just put it in your function name, which is what you did.

Spokes

unread,
Nov 13, 2014, 2:08:45 PM11/13/14
to web...@googlegroups.com
Got it. Thanks!
Reply all
Reply to author
Forward
0 new messages