hundreds-camera-24900
05/02/2022, 7:55 PMfew-vegetable-15970
05/02/2022, 8:14 PMswift-arm-37963
05/05/2022, 1:43 AMhx-delete
is being called twice? One for the endpoint and one again in the redirect url. I changed it to hx-post
and it does not have that behavior.refined-waiter-90422
05/05/2022, 1:59 AMswift-arm-37963
05/05/2022, 2:11 AMwhite-motorcycle-95262
05/05/2022, 5:23 PMtemplate_names
dictionary where keys were targets (element ids) and values were template names. Then in get_template_names
it would try to return self.template_names[self.request.htmx.target]
This lets you use multiple template fragments in one viewhundreds-camera-24900
05/05/2022, 5:51 PMhundreds-camera-24900
05/05/2022, 5:51 PMhundreds-camera-24900
05/05/2022, 5:51 PMhundreds-camera-24900
05/05/2022, 5:52 PMwhite-motorcycle-95262
05/05/2022, 6:12 PMwhite-motorcycle-95262
05/05/2022, 6:12 PM<div class="col">
{% include 'widgets/my_widget.html' %}
</div>
hundreds-camera-24900
05/06/2022, 1:56 AMhundreds-camera-24900
05/06/2022, 1:57 AMwhite-motorcycle-95262
05/06/2022, 12:51 PMget_context_data
to provide a base_template
variable, inspired by https://django-htmx.readthedocs.io/en/latest/tips.html#partial-rendering
context = super().get_context_data(**kwargs)
context.update({
'base_template': 'main.html' if self.request.htmx else 'base.html'
})
Then most of my templates start with {% extends base_template %}
, where base.html
has the <html>
<head>
, <script>
tags etc, and an hx-boosted div
<div id="main-wrapper" hx-boost="true" hx-target="#main" hx-history-elt="true" hx-swap="innerHTML">
{% include 'main.html' %}
</div>
and main.html is
<main id="main">
{% block extra_css %}{% endblock %}
{% block extra_js %}{% endbock %}
{% block main %}{% endblock %}
</main>
few-vegetable-15970
05/06/2022, 6:37 PMfew-vegetable-15970
05/06/2022, 6:41 PMfew-vegetable-15970
05/06/2022, 6:42 PMwhite-motorcycle-95262
05/06/2022, 7:00 PM#main
, but looks like you found that out 🙂ambitious-postman-28532
05/07/2022, 6:47 AMFile-upload-page -> (file parsed in django) -> File-parse-result-page -> Show-parsed-info-in--a-tree-view-page -> (process the selections) -> Show-final-awesomeness-page
I am using partial templates and htmx to move along the sequence.
The problem I'm having is that different pages need different javascript packages and <script>s
in addition to those loaded in the site _base.html
and scripts.html
I've read the docs for htmx.onLoad()
and htmx.process()
multiple times, and I still can't seem to get my head around where I should be putting the <script src=..
tags and the snippets of JS needed to do things like configure the tree display widget I'm using (jstree)
My python and Django knowledge is moderate, my js is very limited.
As an example. I have this tag on the first partial, that is loaded into the base page via a {% include 'import/htmx/upload_file.html' %}
rather than htmx, The script appears to activate on subsequent htmx-swaps
<script>
htmx.onLoad(function () {
bsCustomFileInput.init();
});
</script>
All the docs have stuff like function(elt)
etc, should I be tagging a div or using something specific on the page to trigger the function after the htmx.onLoad()
?
(tldr:) Where should partial page specific js be put? Base page or in the partial.
How can I ensure JS loaded in the partial via htmx is seen in the DOM and can be activated/triggered.
Thanks.few-vegetable-15970
05/07/2022, 8:36 AMhtml
<form {% if request.htmx %}
hx-post="/there" hx-target="#my-element"
{% else %}
method="post" action="/here"
{% endif %}
...
</form>
Then in the back you have to handle the redirection accordingly…few-vegetable-15970
05/07/2022, 8:43 AMpython
class HtmxMixin:
fragment_name: str = None
fragment_names: dict[str] = {}
def get_template_names(self):
if self.request.htmx:
return self.get_fragment_names()
return super().get_template_names()
def get_fragment_names(self):
if self.fragment_name is None not self.fragment_names:
raise ImproperlyConfigured("blabla")
return self.fragment_names.get(self.request.htmx.target,
self.fragment_name)
Now you can subclass and chose between defining only one fragment, or several based on target...ambitious-postman-28532
05/07/2022, 12:03 PMfew-vegetable-15970
05/09/2022, 1:09 PMfew-vegetable-15970
05/09/2022, 1:10 PMbland-coat-6833
05/09/2022, 3:38 PMrefined-waiter-90422
05/09/2022, 3:39 PMmysterious-toddler-20573
05/18/2022, 8:14 PMmagnificent-sandwich-34373
05/19/2022, 8:46 AMkind-kite-734
05/20/2022, 4:45 AM