carltongibson / neapolitan

Quick CRUD views for Django
https://noumenal.es/neapolitan/
MIT License
516 stars 36 forks source link

Using Neapolitan with HTMX #33

Open Yacobolo opened 1 year ago

Yacobolo commented 1 year ago

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

carltongibson commented 1 year ago

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
jefftriplett commented 1 year ago

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