adamchainz / django-htmx

Extensions for using Django with htmx.
https://django-htmx.readthedocs.io/
MIT License
1.6k stars 139 forks source link

Management command for downloading HTMX #87

Closed jordaneremieff closed 2 months ago

jordaneremieff commented 3 years ago

In my project I've created a management command based on https://github.com/adamchainz/django-htmx/blob/main/example/download_htmx.py.

Would it be desirable to include a management command directly in this project for this purpose?

adamchainz commented 3 years ago

No the project deliberately doesn't bundle htmx or provide any way of doing so, since there are too many ways you might want to embed htmx - from a CDN, your own static files, or via a JS bundler npm, and with or without extensions. The download script there is to make the example app more manageable and you're free to adapt it.

adamchainz commented 6 months ago

I reconsidered this after several further requests over the years. #428 has a possible implementation.

kryskool commented 5 months ago

Hi @kytta @adamchainz

if HTMX is an integral part of our development project, it seems useful to me to declare it as a parameter, as Django compressor does

to perform this operation we need to add these news settings:

All news settings have the prefix HTMX_

HTMX_VERSION = "1.9.11"

Indicate the htmx version to download

HTMX_EXTENSIONS = [
    "htmx.js",  # mandatory for django-htmx
    "ext/debug.js",  # mandatory for django-htmx
    "ext/event-header.js",  # mandatory for django-htmx
    "https://github.com/orgname/repos/main/src/",
    "https://raw.githubusercontent.com/bigskysoftware/htmx-extensions/main/src/head-support/head-support.js",
    "https://unpkg.com/browse/htmx-ext@0.1.5/dist/csrf-token.js",
]

By default HTMX_EXTENSIONS have these values:

we need to define where to store the download files

HTMX_STATIC_ROOT = "static_htmx/"
# or
HTMX_STATIC_ROOT = "/opt/django_tuto/static_htmx/"

HTMX_STATIC_ROOT is the original folder to collect the HTMX files with command downloadhtmx or htmx download

HTMX_STATIC_URL = "htmx"

by default the static URL for htmx STATIC_URL + HTMX_STATIC_URL

After call the download hmtx command, we can call the collectstatic command.

STATICFILES_DIRS = [
    ...
    (HTMX_STATIC_URL, HTMX_STATIC_ROOT),
]

On the html template we can use these to include the htmx files

<script src="{% static 'htmx/myjsfile.js' %}"></script> 

With these settings, no need to pass extra arguments on the command line.

just execute commands in this order

python manage.py downloadhtmx (or htmx download)
python manage.py collectstatic

What do you think ?

adamchainz commented 2 months ago

Decided against this in #428. It’s a lot easier to write a script in your project that downloads all your third-party JS/CSS assets, using curl or similar, like the example project does since #468. That way there’s no needless “abstraction” through settings or need for maintaining a database of extension URLs.