Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTMX + Django

HTMX + Django

HTMX is a library to build interactive web applications by accessing the power of “hypertext” directly from HTML. No custom JavaScript! This talk explores the benefits of this architecture and shows how it can be applied to a Django project through simple modifications to your existing HTML templates.

Jace Browning

July 19, 2021
Tweet

More Decks by Jace Browning

Other Decks in Programming

Transcript

  1. Outline • The traditional request-response architecture • Incorporating AJAX to

    build interactive websites • Applying the “HTML over the wire” pattern using HTMX • Demo
  2. # app/views.py def get_items(request): items = Item.objects.all() context = {“items”:

    items} return render(request, “app/index.html”, context)
  3. # app/index.html {% include “app/_navbar.html” %} <p>Items:</p> <ul> {% for

    item in items %} <li> {% include “app/_item.html” %} </li> {% endfor %} </ul> # app/_item.html <p> {{ item.name }} (count: {{ item.count }}) </p>
  4. Typical Progression 1. Add JavaScript to existing HTML templates 2.

    Pull in a component library for rendering (e.g. React) 3. Hand over routing control to the frontend 4. Rewrite all HTML views as JSON APIs (i.e. Django REST Framework viewsets) 5. Completely extract the frontend from the backend
  5. Ideal Use Cases • Rich, dynamic websites • Large teams

    that hire specialists • Multiple, independently-deployable frontends • Products with a mobile or embedded component
  6. Disadvantages • Multiple technology stacks (2+ MVCs) • State managed

    on the frontend and the backend • Potentially duplicated business logic • Backend often viewed as “second-class” to the frontend • Prototyping depends on establishing a data contract
  7. “HTMX allows you to access AJAX, CSS Transitions, WebSockets and

    Server Sent Events directly in HTML, using attributes, so you can build modern user interfaces with the simplicity and power of hypertext.” https://htmx.org
  8. # app/base.html <!DOCTYPE html> <html lang="en"> <body> {% block content

    %}{% endblock content %} <script src="https://unpkg.com/[email protected]"></script> </body> </html>
  9. # app/index.html {% include “app/_navbar.html” %} <p>Items:</p> <ul> {% for

    item in items %} <li> {% include “app/_item.html” %} </li> {% endfor %} </ul> # app/_item.html <p> {{ item.name }} (count: {{ item.count }}) </p>
  10. # app/views.py def get_items(request): items = Item.objects.all() context = {“items”:

    items} return render(request, “app/index.html”, context) def update_item(request, pk): item = Item.objects.get(pk=pk) item.count += 1 item.save() context = {“item”: item} return render(request, “app/_item.html”, context)
  11. Advantages • All logic stays in Python! • Backend is

    solely responsible for stage management • Faster prototyping using existing HTML templates • Simplified testing of partial response endpoints