Can't open a form?

What I’m trying to do:
I’m trying to redirect someone from one form to another if they’re not logged in. It’s so that the user can’t read the data until they’re logged in.

What I’ve tried and what’s not working:

class Home(HomeTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    
    anvil.users.logout()

    self.logged_in = anvil.users.get_user()
    
    if self.logged_in is None:
      print("NONE WTH")
      open_form(Login())
      print("huh")

    # Any code you write here will run when the form opens.

  def text_box_1_pressed_enter(self, **event_args):
    """This method is called when the user presses Enter in this text box"""
    pass

Literally new here and this is all my code; if the user isn’t logged in, they redirect to the Login page which tells them why they should log in first. All my prints are being printed.

Clone link:
share a copy of your app

1 Like

https://anvil.works/build#clone:ZC6MUDPQCCTLW2ZW=C6E44LDDZITOHTMDZYBPNNWG

You know it’s funny, I don’t think I have ever tried to open another form in the __init__ of a form that is loading. I have always had it set to a button or some kind of event.

I don’t think you can do that.

I moved it to a timer component that I set to stop running if it does not need to go to the next form once.

Also you can call open form with a string of the form name, you don’t have to import it as an object. (The auto-completer for open_form() should bring up available forms.)

Also-also:
Welcome to Anvil! :wave:

This post and solution might be helpful

And then once you’ve read about startup modules you’ll probably want to search for that (some useful links):

Anvil Docs | Client Code

Returning to a startup module - #2 by stefano.menci

3 Likes

Ah, I get why it’s not working now. I can’t seem to find form_show in the docs though… how do I use it?

EDIT: ah, I tried out the startup module and it works. Thank you!

2 Likes