How To Delete

This how-to demonstrates how to delete an object using hx-requests. It follows the same structure as Django’s DeleteView.

{% load hx_tags %}
<div id="object_target">{{object}}</div>
<button {% hx_post 'delete_object' object=object %}
        hx-swap="outerHTML"
        hx-target="#object_target"
        type="button">
    Delete
</button>
Notes
  • The object passed in to the hx_post is a Django model instance and will be deleted

class Delete(DeleteHxRequest):
    name = "delete_object"
    return_empty = True
Notes
  • The return_empty attribute ensures that an empty response is returned after deletion which effectively removes the #object_target div from the DOM.

  • If you prefer to update the DOM instead of removing the element, you can use POST_template.