Git Product home page Git Product logo

djhtmx's Introduction

djhtmx

Interactive UI Components for Django using htmx

Install

Add djhtmx to your INSTALLED_APPS and install the Middleware as the last one of the list:

INSTALLED_APPS = [
    ...
    'djhtmx',
    ...
]

MIDDLEWARE = [
    ...,
    'djhtmx.Middleware',
]

And expose the HTTP endpoint in your urls.py as you wish, you can use any path.

from django.urls import path, include

urlpatterns = [
    # ...
    path('__htmx/', include('djhtmx.urls')),
    # ...
]

In your base template you need to load the necessary scripts to make this work

{% load htmx %}
<!doctype html>
<html>
    <head>
        {% htmx-headers %}
    </head>
</html>

Getting started

This app will look for live.py files in your app and registers all components found there, but if you load any module where you have components manually when Django boots up, that also works.

from djhtmx.component import PydanticComponent


class Counter(PydanticComponent):
    template_name = 'counter.html'
    counter: int = 0

    def inc(self, amount: int = 1):
        self.counter += amount

The counter.html would be:

{% load htmx %}
<div {% hx-tag %}>
  {{ counter }}
  <button {% on 'inc' %}>+</button>
  <button {% on 'inc' amount=2 %}>+2</button>
</div>

Now use the component in any of your html templates:

{% load htmx %} Counter: <br />
{% htmx 'Counter' %} Counter with init value 3:<br />
{% htmx 'Counter' counter=3 %}

What batteries are included

This project mixes htmx with morphdom for a more smooth rendering a find control of the focus when this one is on an input.

If you wanna use hx-boost go ahead and enable it with:

...
<body hx-boost="true">
   ...
</body>
...

Python APIs

TODO

Template tags

TODO

Extended htmx attributes

  • hx-after-swap: Add JavaScript here if you want it to be executed when the element is updated.

djhtmx's People

Contributors

edelvalle avatar mvaled avatar nerdoc avatar topiaruss avatar leiserfg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.