hundreds-camera-24900
12/28/2021, 6:40 PMhundreds-camera-24900
12/28/2021, 6:40 PMhundreds-camera-24900
12/28/2021, 6:43 PMhundreds-camera-24900
12/28/2021, 6:44 PMhundreds-camera-24900
12/29/2021, 12:55 AMhundreds-camera-24900
12/29/2021, 12:55 AMhundreds-camera-24900
12/29/2021, 12:56 AMhundreds-camera-24900
12/29/2021, 12:57 AMhundreds-camera-24900
12/29/2021, 12:57 AMhundreds-camera-24900
12/29/2021, 12:58 AMhundreds-camera-24900
12/29/2021, 1:00 AMhundreds-camera-24900
12/29/2021, 1:00 AMprehistoric-smartphone-78535
12/29/2021, 4:45 AM<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
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 itcool-camera-13454
12/29/2021, 8:29 AMhx-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.cool-camera-13454
12/29/2021, 8:31 AMloading=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/hundreds-camera-24900
12/29/2021, 2:31 PMhundreds-camera-24900
12/29/2021, 2:32 PMhundreds-camera-24900
12/29/2021, 2:47 PMhundreds-camera-24900
12/29/2021, 3:50 PMhundreds-camera-24900
12/29/2021, 3:51 PMhundreds-camera-24900
12/29/2021, 3:51 PMhundreds-camera-24900
12/29/2021, 3:53 PMhundreds-camera-24900
12/29/2021, 3:55 PMhundreds-camera-24900
12/29/2021, 3:56 PMgorgeous-airport-54386
12/29/2021, 3:56 PMgorgeous-airport-54386
12/29/2021, 3:57 PMgorgeous-airport-54386
12/29/2021, 3:57 PMhundreds-camera-24900
12/29/2021, 3:58 PMhundreds-camera-24900
12/29/2021, 3:58 PMgorgeous-airport-54386
12/29/2021, 3:58 PMexpress.use(ReactHTMX.render(<App />))