I have an application that uses the shopping cart functionality. The user selects an item and adds it to the cart. Accordingly, in the backend, this item is passed to the database, the cart table. Next, I select all the products that match this user. These products are stored as a list of tuples with data about each product. To display the products in the cart UI, I create a loop in which I iterate over the list of cart items and create a Row element with all the fields (text, buttons) that correspond to a specific product in the cart.
(order.py, prods_list function, parameters: pg(ft.Page), list(cart_list)):
added_buttons = []
for i in cart:
button = ft.Row([
ft.Container([
ft.Image(src=i[3], width=200, height=250, expand=True),
ft.Column([
ft.Text(value=i[2], size=20, weight=ft.FontWeight.W_500),
ft.Text(value=f"{i[-2]} ₽", size=20, weight=ft.FontWeight.W_500),
],
alignment=ft.MainAxisAlignment.SPACE_AROUND),
ft.IconButton(icon="remove", disabled=condition_quantity(i[-1], icon="remove"), data="mashed_potatoes", on_click=quantity_change),
ft.Text(value=i[-1], data="mashed_potatoes", size=20, color="black", weight=ft.FontWeight.W_200),
ft.IconButton(icon="add", disabled=condition_quantity(i[-1], icon="add"), data="mashed_potatoes", on_click=quantity_change),
ft.IconButton(icon="delete", disabled=condition_quantity(i[-1], icon="delete"), data="mashed_potatoes", on_click=quantity_change),
]),
],
)
added_buttons.append(button)
return added_buttons
Then, I add the element to the list of all elements. Then, in the main file, I import this list, add all the elements to ft.Column and display them on the application page.
c = ft.Column(order.prods_list(pg, cart))
pg.add(c)
But when displaying, the following error appears, which I can not fix:
Future exception was never retrieved
future: <Future finished exception=AttributeError("'list' object has no attribute '_set_attr_internal'")>
AttributeError: 'list' object has no attribute '_set_attr_internal'
I tried to use the page output differently, but nothing happened. The error remained.
I have corrected this error. I did this by removing the Row element and replacing all the Container elements with the variables in which they are stored. The Container itself is an integral part of the Row element.
added_buttons = []
for i in cart:
print(i)
image = ft.Image(src=i[3], width=200, height=250, expand=True)
text_column = ft.Column([
ft.Text(value=i[2], size=20, weight=ft.FontWeight.W_500),
ft.Text(value=f"{i[4]} ₽", size=20, weight=ft.FontWeight.W_500),
],
alignment=ft.MainAxisAlignment.SPACE_AROUND)
minus = ft.IconButton(icon="remove", disabled=condition_quantity(i[5], icon="remove"), data="mashed_potatoes", on_click=quantity_change)
quantity = ft.Text(value=i[5], data="mashed_potatoes", size=20, color="black", weight=ft.FontWeight.W_200)
add = ft.IconButton(icon="add", disabled=condition_quantity(i[5], icon="add"), data="mashed_potatoes", on_click=quantity_change)
delete = ft.IconButton(icon="delete", disabled=condition_quantity(i[5], icon="delete"), data="mashed_potatoes", on_click=quantity_change)
button = ft.Container(content=ft.Row([
image,
text_column,
minus,
quantity,
add,
delete
]),
padding=10,
margin=5,
width=820,
height=150,
border_radius=ft.border_radius.all(5),
border=ft.border.all(1, ft.colors.BLACK))
added_buttons.append(button)
print(added_buttons)
return added_buttons
The problem may also lie in the incorrect value of the alignment attribute IN the CONTAINER, where ft.alignment should be specified instead of ft.MainAxisAlignment. It is advisable to specify the alignment markup itself in the main container, which I did before displaying it on the page:
c = ft.Container(
content=ft.Column(order.prods_list(pg, cart)),
alignment=ft.alignment.center)
pg.add(c)