neapolitan icon indicating copy to clipboard operation
neapolitan copied to clipboard

Using Neapolitan with HTMX

Open Yacobolo opened this issue 1 year ago • 2 comments

Hey Carlton - any suggestions on how to integrate this with htmx, or should i consider a different approach?

Yacobolo avatar Nov 15 '23 07:11 Yacobolo

Hi @Yacobolo 👋

So... the first take would be Just use it! 😅 HTMX works great with Neapolitan. I'm using it myself daily, and I highly recommend it.

I guess, the Tip would be to (first-pass) use hx-select to pick just a part of the response to swap-in, and then (bit more advanced) set self.template_name in the view (or overriding get_template_names()) to specify an HTMX specific template.

I created my package django-template-partials in order to aid this second use case. You can define your partials in your main template and then reference them by name later on. I'd recommend checking that out.

As I say, I'm using this daily. I find the combination of Neapolitan's CRUDView, django-template-partials, and HTMX to be really powerful. It gives me pretty much everything I want. I add a sprinkle of JS using Alpine.js, and that's about it. I'm not finding that I need to reach for much more.

I hope that helps?

Links:

  1. https://htmx.org/attributes/hx-select/
  2. https://github.com/carltongibson/django-template-partials
  3. https://alpinejs.dev

carltongibson avatar Nov 15 '23 08:11 carltongibson

django-htmx might be worth a mention too. I have been using it with neapolitan + django-template-partials along with this pattern. (pulled this snippet from a production website)

    def get_template_name(self):
        template_name = "jobs/joblisting_list.html"

        if self.request.htmx:
            template_name = f"{template_name}#job-list-partial"

        return template_name

jefftriplett avatar Nov 15 '23 14:11 jefftriplett