https://htmx.org logo
Join Discord
Powered by
# 🔥-django-htmx
  • h

    hundreds-camera-24900

    05/02/2022, 7:55 PM
    I think what you have is great except you're missing the boosted case
  • f

    few-vegetable-15970

    05/02/2022, 8:14 PM
    Perfect, so I'm not alone on the concept. Thanks a lot!
  • s

    swift-arm-37963

    05/05/2022, 1:43 AM
    hi! Me again. Any idea why
    hx-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.
  • r

    refined-waiter-90422

    05/05/2022, 1:59 AM
    htmx wants to make 100% sure it gets deleted, that's why.
  • s

    swift-arm-37963

    05/05/2022, 2:11 AM
    that makes sense. how should I handle it in the backend? Should I return a 204 instead of 302?
  • w

    white-motorcycle-95262

    05/05/2022, 5:23 PM
    My take was to define a class level
    template_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 view
  • h

    hundreds-camera-24900

    05/05/2022, 5:51 PM
    I returned a 303
  • h

    hundreds-camera-24900

    05/05/2022, 5:51 PM
    instead of a 302
  • h

    hundreds-camera-24900

    05/05/2022, 5:51 PM
    that worked for deletes
  • h

    hundreds-camera-24900

    05/05/2022, 5:52 PM
    I really like that - more dynamic than just htmx/not htmx
  • w

    white-motorcycle-95262

    05/05/2022, 6:12 PM
    Yeah, I have a couple dashboards that have multiple widgets, each widget has it's own template and a method in the larger Class Based View. It's also helpful for if you want to display forms and have a template for the form
  • w

    white-motorcycle-95262

    05/05/2022, 6:12 PM
    Yeah, I have a couple dashboards that have multiple widgets, each widget has it's own template and a method in the larger Class Based View. It's also helpful for if you want to display forms and have a template for the form EDIT: To go further, each widget template is a Bootstrap card, and then I can mess around with layouts by having templates that are basically just
    Copy code
    <div class="col">
      {% include 'widgets/my_widget.html' %}
    </div>
  • h

    hundreds-camera-24900

    05/06/2022, 1:56 AM
    Yeah I did the htmx/not htmx but it didn't occur to me to vary in the Target
  • h

    hundreds-camera-24900

    05/06/2022, 1:57 AM
    That makes total sense
  • w

    white-motorcycle-95262

    05/06/2022, 12:51 PM
    I also override
    get_context_data
    to provide a
    base_template
    variable, inspired by https://django-htmx.readthedocs.io/en/latest/tips.html#partial-rendering
    Copy code
    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
    Copy code
    <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
    Copy code
    <main id="main">
      {% block extra_css %}{% endblock %}
      {% block extra_js %}{% endbock %}
      {% block main %}{% endblock %}
    </main>
  • f

    few-vegetable-15970

    05/06/2022, 6:37 PM
    Very interesting pattern… Thanks for sharing! This is where discord channels make the most sense
  • f

    few-vegetable-15970

    05/06/2022, 6:41 PM
    It does’nt fell that will play well in all cases, swapping the main is not always what you want. Depends on the structure of the site, the structure of the page…
  • f

    few-vegetable-15970

    05/06/2022, 6:42 PM
    This definitely an interesting approach! if in one page you have several htmx interaction, this is very very neat!
  • w

    white-motorcycle-95262

    05/06/2022, 7:00 PM
    Thanks 🙂 I was about to reply to your previous comment saying I don't always target
    #main
    , but looks like you found that out 🙂
  • a

    ambitious-postman-28532

    05/07/2022, 6:47 AM
    Hello everyone, I'm writing a Django app that has a sequence of pages following a file upload. eg:
    Copy code
    File-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
    Copy code
    <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.
  • f

    few-vegetable-15970

    05/07/2022, 8:36 AM
    Tip found today (maybe you thought about it): If you want to update an component on a page through a form that has already a separate view:
    Copy code
    html
    <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…
  • f

    few-vegetable-15970

    05/07/2022, 8:43 AM
    take 2:
    Copy code
    python
    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...
  • a

    ambitious-postman-28532

    05/07/2022, 12:03 PM
    offtopic is there something else to do with the backticks to get discord to colour the code block?
  • f

    few-vegetable-15970

    05/09/2022, 1:09 PM
    yes, it’s a markdown feature I think, just put the name of the language after the three first backsticks
  • f

    few-vegetable-15970

    05/09/2022, 1:10 PM
    ````python`
  • b

    bland-coat-6833

    05/09/2022, 3:38 PM
    (And ````applescript` to get most of the way there with hyperscript)
  • r

    refined-waiter-90422

    05/09/2022, 3:39 PM
    top tip. It's the server secret.
  • m

    mysterious-toddler-20573

    05/18/2022, 8:14 PM
    https://twitter.com/htmx_org/status/1527018695293390851
  • m

    magnificent-sandwich-34373

    05/19/2022, 8:46 AM
    https://www.reddit.com/r/htmx/comments/usyzgn/django_htmx_and_leafletjs/
  • k

    kind-kite-734

    05/20/2022, 4:45 AM
    Hey all, this was a problem I stumbled on yesterday. The solution I found to work for me was to use the django-htmx HttpResponseClientRedirect like in the image attached.
1...596061...100Latest