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

    adamant-exabyte-92636

    12/27/2021, 2:48 PM
    I reckon the 1st
    hx-swap
    should rather be
    hx-trigger
  • a

    adamant-exabyte-92636

    12/27/2021, 2:50 PM
    I.e. you're telling htmx on what event it should do the hx-post. Also note, if you're putting something in the trigger event filter (i.e. the stuff in square brackets after
    click
    in this case), it should return a boolean. So perhaps add a
    return true
    at the end of your testing function
  • b

    blue-gold-89534

    12/27/2021, 2:57 PM
    Copy code
    html
     <button hx-get="{% url 'detail' product.pk %}"
             hx-trigger="click[testing]"
             hx-swap="outerHTML">
                test
    </button>
    Copy code
    JS
    var testing = function () {console.log("test");return true}
    I tried it like this (there was a mistake in the hx events in the first post) and with the hx-trigger in it, the hx-get stops working
  • b

    blue-gold-89534

    12/27/2021, 3:03 PM
    without the hx-trigger it works
  • b

    blue-gold-89534

    12/27/2021, 3:05 PM
    with the hx-trigger neither the trigger nor the get works
  • a

    adamant-exabyte-92636

    12/27/2021, 3:10 PM
    Try taking out the
    [testing]
    and confirm that it works without the filter?
  • b

    blue-gold-89534

    12/27/2021, 3:12 PM
    yes, works
    Copy code
    html
     <button hx-get="{% url 'detail' product.pk %}"
             hx-trigger="click"
             hx-swap="outerHTML">
                test
    </button>
  • a

    adamant-exabyte-92636

    12/27/2021, 3:13 PM
    Ok, so then the next step - is the
    testing
    function variable available in the global scope?
  • a

    adamant-exabyte-92636

    12/27/2021, 3:14 PM
    🤔
  • a

    adamant-exabyte-92636

    12/27/2021, 3:20 PM
    I can't spot anything wrong with that, sorry. Maybe someone else has an idea
  • a

    adamant-exabyte-92636

    12/27/2021, 3:21 PM
    oh!
  • a

    adamant-exabyte-92636

    12/27/2021, 3:21 PM
    Yes I can
  • a

    adamant-exabyte-92636

    12/27/2021, 3:21 PM
    I think you need
    [testing()]
  • a

    adamant-exabyte-92636

    12/27/2021, 3:22 PM
    Yup, pretty sure you need to include the brackets with the function call in the filter
  • a

    adamant-exabyte-92636

    12/27/2021, 3:23 PM
    i.e.
    Copy code
    html
    <button hx-get="{% url 'detail' product.pk %}"
             hx-trigger="click[testing()]"
             hx-swap="outerHTML">
                test
    </button>
  • a

    adamant-exabyte-92636

    12/27/2021, 3:30 PM
    You can see it explained here: https://htmx.org/attributes/hx-trigger/ in the 'Conditions can also refer to global functions or state' example
  • a

    adamant-exabyte-92636

    12/27/2021, 3:31 PM
    @User ☝️
  • b

    blue-gold-89534

    12/27/2021, 3:58 PM
    haha yes that was it!
  • b

    blue-gold-89534

    12/27/2021, 3:59 PM
    i did not think of that.
  • a

    adamant-exabyte-92636

    12/27/2021, 4:00 PM
    I should have spotted it sooner, sorry! That particular detail has bitten me too in the past... I can't believe I'd forgotten about it 🤦‍♂️ 😅
  • w

    white-motorcycle-95262

    12/27/2021, 4:36 PM
    I'm new to HTMX but loving it so far: going through and replacing a lot of JS with it. In one case, I have a form that when submitted, the view returns an X-Accel-Redirect to a file so that nginx serves up the file. At the moment, HTMX is replacing the inside of my submit button with the byte code of the file (which makes sense). I'm using django-htmx, and I sort of know about HttpResponseClientRedirect, but I'm not sure if this is right since the POST request is already doing an X-Accel-Redirect. I suppose there's also 204 - No Content, but that also doesn't feel right either, since there is content being returned, I just don't want it swaped into the DOM anywhere. Should I just not be using HTMX here? I was hoping to use it to submit the form (and show an indicator spinner) without redirecting the page.
  • m

    mysterious-toddler-20573

    12/27/2021, 4:38 PM
    If you use
    HX-Redirect
    to the file URL, it should download properly
  • w

    white-motorcycle-95262

    12/27/2021, 4:43 PM
    hmm, so instead of the X-Accel i can just add an HX-Redirec6t?
  • w

    white-motorcycle-95262

    12/27/2021, 4:44 PM
    (sorry, crying baby in one hand!)
  • w

    white-motorcycle-95262

    12/27/2021, 4:53 PM
    Hmm, so
    self.filepath
    in the image above is the actual location of the file on my server: there's no view or URLConf entry that serves up this file, so I'm not sure HX-Redirect will work, since I think it expects a URL
  • m

    mysterious-toddler-20573

    12/27/2021, 5:23 PM
    correct, is there a way to generate a URL for it?
  • w

    white-motorcycle-95262

    12/27/2021, 5:30 PM
    Well, I was using X-Accel-Redirect because I didn't want the file to be exposed to the public, only available for certain logged in users. It might be that I don't really need HTMX in this situation, since the form submission will be returning the desired response anyways
  • m

    mysterious-toddler-20573

    12/27/2021, 5:55 PM
    Ah, got it. I am not familiar w/ that header, but it sounds like you are on the right track.
  • w

    white-motorcycle-95262

    12/27/2021, 6:06 PM
    Looking forward to contributing a bit around here, really enjoying what I've learned about HTMX so far
  • p

    prehistoric-cat-63987

    12/27/2021, 9:58 PM
    Worked like magic. Thanks allot.
1...282930...100Latest