URL acts differently when called inside a module

32 views
Skip to first unread message

jonatha...@whatho.net

unread,
Jul 10, 2023, 12:47:41 PM7/10/23
to py4web
Hello,

I have a bit of code which builds a navbar. It has lots of 
A('text',  _href=URL('path_piece')) 
pieces. I have noticed that if the call to URL is in a template or the controllers.py file, then the result of a call like URL('path_piece') is "/app_name/path_piece". If the call is from a module imported into controllers.py then the result is just "/path_piece".

Is it expected that URL returns different results depending on whether it is called from within controllers.py or a template file, vs when it is called within a module? It seems much neater to put this code in a module, but perhaps I can't.

Thanks,
Jonathan




Jim Steil

unread,
Jul 10, 2023, 2:39:13 PM7/10/23
to py4web
Can you write up a minimal app that shows the behavior?

-Jim

Massimo

unread,
Jul 11, 2023, 12:19:12 AM7/11/23
to py4web
I can see that happening but not quite under the condition you outline.
URL() needs to figure out which what app_name is. It cannot determine that from it is location.
It determines it from the chain of events that lead to its execution.

So if it is called while processing a request, whether in the controller or in an imported module, than it should work as intended.
if you call outside of the context of a request (for example in a controller, outside an action) than it may produce a wrong result.
It should raise an exception instead. Will fix this.

If your case is different, indeed, provide an example.

Massimo
Reply all
Reply to author
Forward
0 new messages