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

    hundreds-camera-24900

    12/28/2021, 6:40 PM
    so I am working on a project template that uses django+htmx and I just made a small QoL fix I wanted to share
  • h

    hundreds-camera-24900

    12/28/2021, 6:40 PM
    https://github.com/Lightmatter/generic-django-conf/commit/21ce7541841a8833e06eef1ff1d440a21a0bd43f
  • h

    hundreds-camera-24900

    12/28/2021, 6:43 PM
    That will fix django-debug-toolbar to be htmx aware
  • h

    hundreds-camera-24900

    12/28/2021, 6:44 PM
    specifically with boosted links
  • h

    hundreds-camera-24900

    12/29/2021, 12:55 AM
    Ok actually getting debug toolbar and htmx to play 100% correctly together was more work than I expected
  • h

    hundreds-camera-24900

    12/29/2021, 12:55 AM
    https://github.com/jazzband/django-debug-toolbar/pull/1569
  • h

    hundreds-camera-24900

    12/29/2021, 12:56 AM
    it works pretty well though I gotta say
  • h

    hundreds-camera-24900

    12/29/2021, 12:57 AM
    you click around triggering hx events and you get sql/cache/cpu timing information on just the htmx request and it slides right in
  • h

    hundreds-camera-24900

    12/29/2021, 12:57 AM
    there's a history tab in the toolbar as well that lets you go back and see a log of all the hx events and the initial load
  • h

    hundreds-camera-24900

    12/29/2021, 12:58 AM
    I think the next thing to do with this would be to make a panel that snapshots dom state right before the hx event and associates that with the history tab so you can do time travel debugging
  • h

    hundreds-camera-24900

    12/29/2021, 1:00 AM
    gonna work on that this weekend after I make some more progress on the template
  • h

    hundreds-camera-24900

    12/29/2021, 1:00 AM
    and hopefully they merge
  • p

    prehistoric-smartphone-78535

    12/29/2021, 4:45 AM
    What do you think about this integration to lazy load images on a page using django? # html to load image (send a post with img source and image dimensions)
    Copy code
    <div hx-post="{% url 'lazy_image' %}" 
          hx-vals='{"src": "{% static 'avatars/first.jpg' %}", "height": "280", "width": "320"}' 
          hx-trigger="load">
    
          <img class="htmx-indicator" width="150" src="/img/bars.svg"/>
    </div>
    # view to return the image html
    Copy code
    def lazy_image(request):
        src = request.POST.get('src', None)
        height = request.POST.get('height', None)
        width = request.POST.get('width', None)
        img = f'<img style="padding-right: 1vw;" height="{height}" width="{width}" src="{src}">'
        return HttpResponse(img)
    Does this makes sense for you? im not sure if this is a bad practice or if there is a better way to do it *I dont like that it makes 1 extra request, but im not sure it thats the only way of doing it
  • c

    cool-camera-13454

    12/29/2021, 8:29 AM
    Hi! I think I understand your implementation, using the
    hx-post
    to request the image tag from the
    lazy_image
    view. I haven't tried it but at first glance I think it could do the trick. However, I don't understand why you are POSTing with
    hx-post
    instead of GETting the image (with
    hx-get
    ). Usually POST requests are used when you want to modify a resource in the server, whereas GET requests make more sense when you want to fetch information from the server (in your case, the image tag). I think it would make more sense to use
    hx-get
    here. If you need to pass parameters to the GET request (as you would with regular query parameters), I believe you can use https://htmx.org/attributes/hx-params/ for that purpose.
  • c

    cool-camera-13454

    12/29/2021, 8:31 AM
    Another thing I've realized: can you not use simply the
    loading=lazy
    attribute for your
    <img>
    tags? It seems to be well supported nowadays (except for Safari): https://caniuse.com/loading-lazy-attr Here's an article about the topic: https://addyosmani.com/blog/lazy-loading/
  • h

    hundreds-camera-24900

    12/29/2021, 2:31 PM
    yeah agreed that just using the loading=lazy attribute is probably easier
  • h

    hundreds-camera-24900

    12/29/2021, 2:32 PM
    if you wanted to go the htmx way my suggestions would be - use get instead of post, remove the outer div and apply the hx attributes directly to the img attribute and have it replace itself on load
  • h

    hundreds-camera-24900

    12/29/2021, 2:47 PM
    if you're looking for optimization choices you might want to check out https://htmx.org/extensions/preload/ as well
  • h

    hundreds-camera-24900

    12/29/2021, 3:50 PM
    Hey something I'd really like to think/talk about is a partial render library like https://github.com/5monkeys/django-viewlet or https://github.com/utapyngo/django-render-partial
  • h

    hundreds-camera-24900

    12/29/2021, 3:51 PM
    it seems pretty clear to me that's a really strong synergy between htmx and something like that - create a component and use it as part of a bigger page or directly through htmx in a single decorator
  • h

    hundreds-camera-24900

    12/29/2021, 3:51 PM
    I haven't used either of those before and I'm curious if anyone else has, and has opinions/ideas
  • h

    hundreds-camera-24900

    12/29/2021, 3:53 PM
    throwing out some ideas I think there could be some kind of {% htmx_refresh %} tag that would take a viewlet/component/partial and would automatically add the url/htmx attribute to allow refresh of a component?
  • h

    hundreds-camera-24900

    12/29/2021, 3:55 PM
    or am I not thinking big enough, and there might be some way to write up a full page template w/o thinking about htmx fragments at all, that then automatically removes and renders the sections you need as the page state changes?
  • h

    hundreds-camera-24900

    12/29/2021, 3:56 PM
    I haven't really thought through what that would look like from an authoring pov
  • g

    gorgeous-airport-54386

    12/29/2021, 3:56 PM
    you're basically inventing a frontend framework but on the backend
  • g

    gorgeous-airport-54386

    12/29/2021, 3:57 PM
    i wonder if it would be possible to run react on the server, and send changes to the client via htmx
  • g

    gorgeous-airport-54386

    12/29/2021, 3:57 PM
    i hope not
  • h

    hundreds-camera-24900

    12/29/2021, 3:58 PM
    lol yes it would but I think that's unneeded
  • h

    hundreds-camera-24900

    12/29/2021, 3:58 PM
    you could do it w/ like next.js or one of the SSR frameworks
  • g

    gorgeous-airport-54386

    12/29/2021, 3:58 PM
    express.use(ReactHTMX.render(<App />))
1...293031...100Latest