https://htmx.org logo
Join Discord
Powered by
# πŸ”₯-django-htmx
  • k

    kind-kite-734

    05/20/2022, 4:49 AM
    There is probably better ways, but I am new to python/django/html
  • k

    kind-kite-734

    05/20/2022, 4:57 AM
    This is probably a simple question and I am missing something obvious. If I load in some new HTML with a hx-get, any javascript that is on the page stops working. I am just using AlpineJS for drop down menus, like this https://larainfo.com/blogs/create-dropdowns-alpine-js-with-tailwind-css The AlpineJS script is loaded in by base.html, so I am not loading any new JS. I have spent hours trying to work this out, but I am not sure where I am going wrong. JS is completely foreign to me.
  • s

    swift-arm-37963

    05/20/2022, 4:58 AM
    My solution was to change the status_code from 302 to 303.
  • i

    important-winter-64905

    05/20/2022, 11:24 AM
    I have a repeating
    input
    form element that I would like to include more info in the htmx request but I'm not sure how to do it. currently:
    Copy code
    <input hx-target="#abc_this" hx-swap="outerHTML swap:0s" 
             class="form-control form-control-sm" type="number" 
             name="nameabc" value="0.00" step="0.5" 
             hx-post="/projects/update" hx-trigger="change delay:0.1s">
    I'd like to add more attributes to the htmx request than are here: https://www.w3schools.com/html/html_form_attributes.asp Does anyone know how to do this?
  • i

    important-winter-64905

    05/20/2022, 11:29 AM
    πŸ“ edit: I ended up finding a better solutions with
    hx-vals
    here --> https://htmx.org/attributes/hx-vals/ ==================== fun when just asking makes you rethink your google search and you come up with magic https://htmx.org/attributes/hx-include/ example
    Copy code
    <div>
        <button hx-post="/register" hx-include="[name='email']">
            Register!
        </button>
        Enter email: <input name="email" type="email"/>
    </div>
  • b

    bland-coat-6833

    05/20/2022, 12:29 PM
    If you replaced the
    div
    with a
    form
    would it not work without the
    hx-include
    ?
  • i

    important-winter-64905

    05/20/2022, 1:46 PM
    I can’t do that because of limitations around putting forms in tables.
  • b

    bland-coat-6833

    05/20/2022, 1:47 PM
    Ah. Fair enough. Carry on πŸ˜€
  • c

    calm-queen-64495

    05/21/2022, 7:18 AM
    Im using DataTables, hx-get on the tbody of the table works fine, however when you search the table it clears, I know htmx.process needs to be used but not sure where to put it, anyone used DataTables with HTMX?
  • r

    refined-waiter-90422

    05/23/2022, 11:49 PM
    https://github.com/adamchainz/talk-django-and-htmx
  • r

    refined-waiter-90422

    05/23/2022, 11:49 PM
    Adams talk is looking sweet.
  • r

    refined-waiter-90422

    05/23/2022, 11:50 PM
    Will want to tune into that for sure
  • m

    mysterious-toddler-20573

    05/24/2022, 3:33 AM
    very cool talk @high-toothbrush-44006 !
  • h

    high-toothbrush-44006

    05/24/2022, 8:13 AM
    I'm afraid this talk will be in person only!
  • h

    high-toothbrush-44006

    05/24/2022, 8:14 AM
    Perhaps I could do it online at a later date
  • r

    refined-waiter-90422

    05/24/2022, 8:26 AM
    so jelly of the audience, good luck!
  • h

    high-toothbrush-44006

    05/24/2022, 2:52 PM
    I added the pdf of the slides in the repo ^
  • c

    calm-queen-64495

    05/25/2022, 1:53 AM
    Did you end up solving this?
  • i

    important-winter-64905

    05/25/2022, 2:11 AM
    I feel like this is the perfect level to pitch the beauty of htmx to my team πŸ™‚
  • i

    important-winter-64905

    05/26/2022, 12:53 PM
    if you're reading this later, the correct HTMX attribute to use is
    hx-vals
    . At least I was able to get this attribute to work without any issues where
    hx-include
    had some issues with CSS selectors (or something like that).
  • s

    silly-bear-76516

    06/06/2022, 3:45 PM
    Hey everyone, I'm doing a PoC on how to do partial/inline form validation with django, trying to keep it as simple as possible. Here is the result so far: I'm using
    hx-select
    &
    hx-trigger=blur
    to only replace current input element with the validation that is sent from the server. How the view looks like:
    Copy code
    python
    class SignUpView(FormView):
        form_class: forms.Form = SignupForm
        template_name: str = 'form-demo.html'
        success_url = reverse_lazy('form-demo')
    
        def form_valid(self, form):
            if self.request.htmx:
                return self.render_to_response(self.get_context_data(form=form))
            return super().form_valid(form)
    How the template looks like:
    Copy code
    html
        <form method="post">
          {% csrf_token %}
    
          <div
              id="name_field" {% if form.name.errors %}class="error"{% endif %}
              hx-select="#name_field"
              hx-post="{% url 'form-demo' %}"
              hx-trigger="blur from:find input"
              hx-target="#name_field">
            {{form.name.label_tag}}
            {{form.name}}
            {{ form.name.errors }}
          </div>
    
          <div
              id="email_field"
              {% if form.email.errors %}class="error"{% endif %}
              hx-select="#email_field"
              hx-post="{% url 'form-demo' %}"
              hx-trigger="blur from:find input"
              hx-target="#email_field">
            {{form.email.label_tag}}
            {{form.email}}
            {{ form.email.errors }}
          </div>
    
          <div id="repeat_email_field"
              {% if form.repeat_email.errors %}class="error"{% endif %}
              hx-select="#repeat_email_field"
              hx-post="{% url 'form-demo' %}"
              hx-trigger="blur from:find input"
              hx-target="#repeat_email_field">
            {{form.repeat_email.label_tag}}
            {{form.repeat_email}}
            {{ form.repeat_email.errors }}
          </div>
    
          <input type="submit" value="Submit">
        </form>
  • s

    silly-bear-76516

    06/06/2022, 3:47 PM
    Any feedback is welcome, maybe I would like to DRY the template part a little bit more....
  • h

    hundreds-camera-24900

    06/06/2022, 6:28 PM
    I would love to get a definitive way to do this and add to the django-htmx docs
  • h

    hundreds-camera-24900

    06/06/2022, 6:28 PM
    that is much easier than I expected
  • s

    silly-bear-76516

    06/06/2022, 6:45 PM
    I'm basically wrapping each form field with a
    div
    + some
    hx-
    directives. The backend is still pretty agnostic and I'm just accounting for the case where the form is valid to render the template instead of redirecting or making any change in the database. The submit action is still expected from the user and follows the normal
    FormView
    flow...
  • h

    hundreds-camera-24900

    06/06/2022, 7:18 PM
    I think the only suggestion I have is something to more obviously mark that this is validate only
  • h

    hundreds-camera-24900

    06/06/2022, 7:18 PM
    if self.request.htmx
    could apply for submitting the form through htmx or a boosted thing
  • h

    hundreds-camera-24900

    06/06/2022, 7:19 PM
    so I might apply an hx-val of "validate=true" or something
  • h

    hundreds-camera-24900

    06/06/2022, 7:53 PM
    you might be able to reuse the field for the hx-select as well
  • h

    hundreds-camera-24900

    06/06/2022, 7:53 PM
    something like
    hx-select={{form.field.id}}
1...606162...100Latest