We’ll write the function for deleting articles from the Data Table inside a Server Module.

We’re using a Server Module because code in Server Modules can be trusted, and we might want to add authentication or other logic to this delete code. We recommend doing it this way for all CRUD apps.

Add this function to your Server Module:

@anvil.server.callable
def delete_article(article):
  # check that the article being deleted exists in the Data Table
  if app_tables.articles.has_row(article):
    article.delete()
  else:
    raise Exception("Article does not exist")

As with the Update function, we’ve added a security check to ensure that the article actually exists in the Data Table. If not, the server function will raise an exception. Without this check, a malicious user could delete any row from any table by passing it to this function.

We’ll call this server function from our Homepage rather than the ‘ArticleView’ Form. It’s a good idea to keep the CRUD operations on the main Form (our Homepage in this case) where possible.

The delete button is on the ‘ArticleView’ Form, but when it’s clicked, we want to call a function on our Homepage Form. We do this by raising an event on the ArticleView’s parent container.

You can look up any component’s parent container with the .parent attribute. In this case, all our ‘ArticleView’ instances are inside the articles_panel on our HomePage. So we can access articles_panel from within any ‘ArticleView’ on this page, and raise an event on it.

self.parent when called on the ArticleView Form refers to the articles_panel on the Homepage.

self.parent when called on the ArticleView Form refers to the articles_panel on the Homepage.

Next, we’ll set a custom event handler on the articles_panel on our Homepage. Then, we can raise this custom event when the delete button on our ‘ArticleView’ Form is clicked, and catch it on our Homepage.

Go back to your ‘Homepage’ Form, and add the following line to the __init__ method of your Form:

  # Set an event handler on the RepeatingPanel (our 'articles_panel')
  self.articles_panel.set_event_handler('x-delete-article', self.delete_article)

This will call the self.delete_article function on the Homepage when the x-delete-event is raised, so let’s create the self.delete_article function. Add this to your Homepage Form:

  def delete_article(self, article, **event_args):
    # Delete the article
    anvil.server.call('delete_article', article)

We’ll call this function when the delete button on our ‘ArticlesView’ Form is clicked, so go back to your ‘ArticlesView’ Form, and edit your delete_article_button_click to look like this:

  def delete_article_button_click(self, **event_args):
    # Get the user to confirm if they wish to delete the article
    # If they confirm, raise the 'x-delete-article' event on the parent (which is the articles_panel on our Homepage)
    if confirm("Are you sure you want to delete {}?".format(self.item['title'])):
      self.parent.raise_event('x-delete-article', article=self.item)

The delete button on our ‘ArticlesView’ Form will now raise the ‘x-delete-event’ on our articles_panel, which we just configured to call the self.delete_article function on the Homepage. This will call our server function and delete the article.