Assign startup-form dynamically based on url hash

I would like to be able to open a separate form in case the request comes with a secret hash token.

My first approach was to store the hash token with:

token = get_url_hash()

and then open Form2 with:

f = Form2()
open_form(f)

However this does not seem to work from the _ init _ method of my form.

And doing this on_show is not a solution because I need to redirect before Form1 “runs”.

Any suggestions?

Best,
Ruben

Not a proper answer, but a workaround i use is to have the startup form as just a dummy (just shows “Please wait …”). Then in the on_show I either load form2 or form3 depending on the url parameters.

2 Likes

Yeah, I guess I should have thought of this :stuck_out_tongue:

Not quite perfect, but just using the first form to “route” the requests seems to be alright

This is indeed the recommended method.

The long-term plan is to be able to specify a “startup Module” instead of a “startup form”, and have that module explicitly open_form() the correct form.


(Tech note: The reason that open_form() within __init__() doesn’t do what you expect is that Anvil creates the startup form before it opens it. If you call open_form() in the startup form’s constructor, the new form will open - but as soon as __init__ returns, it will be replaced by the original startup form you’re constructing. The show event, by contrast, occurs after the form is opened, so open_form() works as you’d expect.)