Form not found - import error

@meredydd
hi. I have started practicing anvil very recently. I am facing issue in navigation between two forms. I ran the code

from ._anvil_designer import Form1Template
from anvil import *
from Form2 import Form2

class Form1(Form1Template):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

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

  def link_1_click(self, **event_args):
    """This method is called when the link is clicked"""
    form2=.Form2()
    open_form(form2) 

but it shows the following error:
ImportError: No module named Form2
plz help.

Hello and welcome,

Is this the exact code? I ask because you have .Form2 (notice the dot) and this would lead to a syntax error AFAIK.

There are a couple ways to open new forms depending on how you have things set up.

If you just want to open a new page and you don’t need the navigation side bar or any of the other Material design-specific details you can just use:

open_form('form_2') # the string is your form's name

If you are using the Material design and want to clear its main content panel and replace it with a new form, you can do this in general:

from ..Form2 import Form2 # this is likely what you were missing

.
.
.

form2=Form2()
self.content_panel.clear()
self.content_panel.add_component(form2)

Here is a clone that demonstrates this concept:
https://anvil.works/build#clone:S7VZI3LMALYDP7AT=K74MJOGULMXVPEMADCUDKJHZ


Other notes:

I’ve moved this to a its own topic. It is not useful to other users to reply on old and unrelated topics.

You do not need to ping Anvil staff with “@someone” as they monitor the forum very closely.

You can format your code nicely on the forum by wrapping your code in bacticks. Example,

```python

print('this will be nicely formatted and easy to read')

```
5 Likes