I’m trying to goto another component through a button click from a component. So i’m currently following this tutorial -
Building data-driven web apps is tough. Yes, Python makes it easier than most languages, but you need to know a host of different languages and technologies and they have to fit together <em>just</em> right. This usually includes Python, HTML, CSS,...
But i’m getting the following error.
Code Sample:
from .. import navigation
Error:
AttributeError: module 'Material_Design_4' has no attribute 'navigation'
at HomeForm, line 8
Clone link:
https://anvil.works/build#clone:AKUPLX6I3GXOBN2G=HZ6Q3RT5PEJFXIX4X57QVBS6
Is there any simple way to do it without using anvil server?
You can’t import a server module from a form. What you want is a client module, not a server module.
1 Like
I didn’t get it. Can you please elaborate? How can i navigate to another component through the button click?
By the way I am doing exactly what Michael Kennedy is doing in this tutorial.
You can create modules in two places…on the client side (by pressing the + next to Client Code) or the server (by pressing the + next to Server Code).
You can only import client side modules in forms, so your navigation module should have been created on the client side, not the server side.
I can’t comment on the tutorial since I haven’t gone through it. I don’t know how up to date it is with the current version of Anvil.
Please check out this post which relates to the tutorial.
If you want to be redirected to home after the user clicks “add measurement” you could do:
def button_save_click(self, **event_args):
.
.
.
# this gives you a reference to the main form
get_open_form().link_home_click()
I have not been able to import navigation inside your subform based on your setup for some reason (there might be an issue with circular imports , or a bug) but you should have access to its various functions via your main form.
If I had to guess I’d say that whe…
1 Like
Thank you for the response. I did a little bit mistake there i see it now.
Fixed everything but i got stuck again somewhere (probably in the client module ‘navigation’), and unable to fix it.
Error is indicating the first line of my client module ‘navigation’:
Error:
ValueError: Attempted relative import beyond toplevel package
at navigation, line 1
called from HomeForm, line 3
Clone link:
https://anvil.works/build#clone:AKUPLX6I3GXOBN2G=HZ6Q3RT5PEJFXIX4X57QVBS6
You’re right. I was trying to import server module by mistake
For your imports, let the autocomplete help you out. Start typing from AddMeasure
and pick the choice from the list that matches the import you want.
1 Like
Thank you. I did exactly what you said and it solved the problem. But got me stuck on the next one
You can see in the following screenshot, it suggested to import navigation. And then allowed me to access the function navigation.go_home()
.
But then i got this error:
AttributeError: module 'Fitnessd' has no attribute 'navigation'
at AddMeasurementComponent, line 3
called from navigation, line 1
called from HomeForm, line 3
Looks like you’re getting into a circular import situation, where HomeForm imports navigation, which imports AddMeasurementComponent, which then tries to import navigation again. The solution I normally see for that is to import at the function level in AddMeasurementComponent (and not at the module level), so something like:
def button_1_click(self, **event_args):
from .. import navigation
navigation.go_home()
Or to redesign things so you don’t need the circular import.
3 Likes
It solved the problem. Thank you very much!!
1 Like