DropDown Items syntax error

Very basic newbie error in data table example.

Any help from anvil gurus gratefully received. I’m trying to work through @meredydd video on this subject. But I’m clearly missing something.

I thought I was doing so well. I’ve got the drop down nicely. And yet, yet… I’m seeing a ‘bad syntax’ error that I know is obvious but I don’t seem to be able to find.

MY APP:
https://anvil.works/build#clone:DOZ7DPPS3O24UGXC=7RON7FP6GTFX2IMKOQPH5OHS

MY CODE:


from ._anvil_designer import HomeTemplate
from anvil import *
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables

class Home(HomeTemplate):
  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.
    
    self.category_dd.items = [(row['entry_category'], r) for r in app_tables.categories.search()]
    

  def button_save_click(self, **event_args):
    """This method is called when the button is clicked"""
    app_tables.journal_entries.add_row(Journal entry=self.entry_text.text, category=self.category_dd.selected_value)

Any help as ever gratefully received.
:crazy_face:

looks like you’ve misnamed the variable in your list comprehension

self.category_dd.items = [(row['entry_category'], r) for r in app_tables.categories.search()]

should be

self.category_dd.items = [(r['entry_category'], r) for r in app_tables.categories.search()]

Thanks Stu. Annoyingly that was another error I put in trying to fix the first! The error seems to be in the very last line of the code.


from ._anvil_designer import HomeTemplate
from anvil import *
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables

class Home(HomeTemplate):
  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.
    
    self.category_dd.items = [(r['entry_category'], r) for r in app_tables.categories.search()]
    

  def button_save_click(self, **event_args):
    """This method is called when the button is clicked"""
    app_tables.journal_entries.add_row(Journal entry=self.entry_text.text, category=self.category_dd.selected_value)

ah yes - I see that you’ve got spaces in your datatable column names

I would change your column names to be something like journal_entry this way your code will work since you can use column names as variable names
(python doesn’t allow spaces in variable names)