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

    victorious-room-74725

    09/01/2021, 5:53 PM
    It can look something like this:
    Copy code
    python
    #urls.py: 
        ...
        path('<int:pk>/some-page/', some_page, name='some_page'),
        path('<int:pk>/do-math/', math_html_fragment, name='do_math'),
        ...
    
    #views.py:
    def some_page(request):
      # Typical rendered html page, contains the html with htmx tags
      return render(request, 'some-page.html', ...)
    
    @require_POST
    def math_html_fragment(request): 
      # Do stuff with request.POST dict
      dummy_value = 5  
      ...
      return HttpResponse(dummy_value)
    
    #some-page.html
    ...
    <some-tag hx-post="{% url 'do_math' (some pk) %}"> 
    ...
  • v

    victorious-room-74725

    09/01/2021, 5:54 PM
    there's not really a predefined way of doing it. you have all the flexibility of django.
  • i

    important-winter-64905

    09/01/2021, 6:06 PM
    Thanks that was a helpful skeleton. I appreciate the time it took you to write!
  • b

    blue-manchester-57655

    09/10/2021, 11:58 PM
    Submitted the video for my DjangoCon video on using htmx with Django, and now I'm super nervous 😬 But yay! Three different htmx-related talks this year at DjangoCon. So if I tanked it, at least there are two others to keep the idea float 🤣 🤞
  • i

    important-winter-64905

    09/11/2021, 10:00 PM
    @User and @User thanks for the encouragement. I've figure out several different way I can do some interesting things with Django and htmx
  • v

    victorious-room-74725

    09/12/2021, 5:15 AM
    Awesome! best of luck!
  • s

    straight-breakfast-8334

    09/12/2021, 9:35 PM
    Hey folks, got a question and wondering whether I could get a bit of help. I've got a and I want to inject in different HTML pages (partials) into the page based on what's been selected. My question is, is it possible to do this without creating a form?
  • u

    user

    09/12/2021, 9:40 PM
    hx-get
    in a
    <select>
    and the use of OOTB swap should do it, I guess: https://htmx.org/attributes/hx-swap-oob/
  • u

    user

    09/12/2021, 10:19 PM
    No form nor a submit needed.
  • s

    straight-breakfast-8334

    09/12/2021, 10:24 PM
    I have something like:
    Copy code
    <select name="stage" hx-get="{% url '<url here>' %}" hx-target="#onboarding-steps" hx-indicator=".htmx-indicator">
                            <option>option 1</option>
    So the suggestion is to add hx-swap-oob="true" in there?
  • m

    mysterious-toddler-20573

    09/12/2021, 10:25 PM
    No, that looks right to me
  • m

    mysterious-toddler-20573

    09/12/2021, 10:25 PM
    hx-swap-oob
    is placed on the returned content: https://htmx.org/attributes/hx-swap-oob/
  • m

    mysterious-toddler-20573

    09/12/2021, 10:25 PM
    If you want to target areas other than the element called out by
    hx-target
  • m

    mysterious-toddler-20573

    09/12/2021, 10:26 PM
    with content in addition to the "normal" content you are returning
  • w

    white-garden-37887

    09/12/2021, 10:26 PM
    > Greetings. I am experimenting with hx-ws Websockets feature and struggling with getting the response data. I the example there is description that Content that is sent down from the websocket will be parsed as HTML and swapped in by the id property, using the same logic as Out of Band Swaps. Is there any suggestion to handle repsonse dict? what is the parsed HTMl like?
  • m

    mysterious-toddler-20573

    09/12/2021, 10:26 PM
    (hence "out of band": the content is swaped out of the normal "band" of content)
  • w

    white-garden-37887

    09/12/2021, 10:32 PM
    Copy code
    <div hx-ws="connect:/ws/game/{{ universe_id }}/update/{{ object }}/">
                        <div id="swap-test" hx-swap="innerHTML">
                            {{ first_call }}
                            Swap Test
                        </div>
                        <button hx-ws="send" class="card-footer-item" id="update-{{ object }}">
                            <i class="fas fa-angle-double-up" aria-hidden="true"></i>
                        </button>
                    </div>
  • w

    white-garden-37887

    09/12/2021, 10:32 PM
    This was my try
  • w

    white-garden-37887

    09/12/2021, 10:33 PM
    I have added a div with id="swap-test" as first item in body but nothing rendered in my template
  • m

    mysterious-toddler-20573

    09/12/2021, 10:33 PM
    Hi there, yes, htmx expects content in HTML form
  • m

    mysterious-toddler-20573

    09/12/2021, 10:34 PM
    let me look at the code and see if the ability to transform responses is in place for it, as with ajax requests
  • w

    white-garden-37887

    09/12/2021, 10:34 PM
    the url is rendered correctly and I got a response dictionary from websocket.
  • w

    white-garden-37887

    09/12/2021, 10:34 PM
    the url is shown with django template tags
  • m

    mysterious-toddler-20573

    09/12/2021, 10:34 PM
    OK, yes: https://github.com/bigskysoftware/htmx/blob/4a02e92e4bc8711dea22d32d130b473019b6781e/src/htmx.js#L1175
  • m

    mysterious-toddler-20573

    09/12/2021, 10:35 PM
    we do give a websocket a chance to transform data (I think this is why you mean when you say dictionary, just a JSON object?) into HTML
  • m

    mysterious-toddler-20573

    09/12/2021, 10:36 PM
    You would need to write an extension similar to client-side templates (or just use that extension if one of those templating libraries is acceptable for you): https://htmx.org/extensions/client-side-templates/
  • m

    mysterious-toddler-20573

    09/12/2021, 10:37 PM
    Generally, htmx works with the server in HTML form, so if it is getting back anything else (e.g. JSON) you need to use an extension to transform it into content htmx can work with
  • w

    white-garden-37887

    09/12/2021, 10:40 PM
    yes, it is a JSON object which is send back from Consumer to Websocket
  • m

    mysterious-toddler-20573

    09/12/2021, 10:41 PM
    OK, so a bit more work then. Sorry about that, but htmx is html-oriented by default. Definitely doable though.
  • w

    white-garden-37887

    09/12/2021, 10:41 PM
    thanks for the hint I will ahve a deep look into extensions. seems to be what I need
1...567...100Latest