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

    victorious-cat-12942

    10/06/2022, 8:04 PM
    Anyone have a good example of a way to swap form choices based on a previous form selection
  • a

    ancient-shoe-86801

    10/06/2022, 8:20 PM
    isn't there on the docs?
  • a

    ancient-shoe-86801

    10/06/2022, 8:20 PM
    https://htmx.org/examples/value-select/
  • v

    victorious-cat-12942

    10/06/2022, 8:32 PM
    I’ll have to see if it can be done with Django dorms
  • v

    victorious-cat-12942

    10/06/2022, 8:32 PM
    Forms
  • g

    green-activity-6102

    10/06/2022, 8:57 PM
    to change a field's queryset you just do something like
    form.fields['field_name'].queryset = Model.objects.filter(...)
  • v

    victorious-cat-12942

    10/06/2022, 9:17 PM
    @green-activity-6102 the only issue I have is the field choices aren’t coming from a model but instead being returned from a api call
  • v

    victorious-cat-12942

    10/06/2022, 9:17 PM
    So I assume I can do what you stated above but after the equal sign slap the api call
  • g

    green-activity-6102

    10/06/2022, 9:17 PM
    same procedure but change
    .choices
    instead of
    .queryset
  • g

    green-activity-6102

    10/06/2022, 9:18 PM
    note that choices is expecting a list of 2-tuples
  • v

    victorious-cat-12942

    10/06/2022, 9:18 PM
    Thanks I’ll give it a try in a little bit
  • v

    victorious-cat-12942

    10/06/2022, 9:18 PM
    Yeah I have a default choice field if the function fails to return correct data
  • v

    victorious-cat-12942

    10/06/2022, 9:19 PM
    So I need like [(“this is one”, 1), (“this is two”, 2)]
  • g

    green-activity-6102

    10/06/2022, 11:18 PM
    correct -- the field needs a human-readable value for display + the underlying value that is sent to the backend when the form is submitted
  • v

    victorious-cat-12942

    10/06/2022, 11:21 PM
    What htmx tag do I have to wrap around the first drop down
  • v

    victorious-cat-12942

    10/06/2022, 11:21 PM
    Also the Django form is type forms.Select
  • g

    green-activity-6102

    10/06/2022, 11:23 PM
    how you structure your HTMX really depends on how you want your application to function
  • v

    victorious-cat-12942

    10/06/2022, 11:27 PM
    I have limited use with htmx so what would you suggest. I have 8 fields on a form and one of them needs to trigger a different one to populate with respective choices the rest are static or keyboard input
  • e

    early-camera-41285

    10/06/2022, 11:35 PM
    Polling has been a great solution for me.
  • v

    victorious-cat-12942

    10/07/2022, 1:01 PM
    @green-activity-6102 when I do forms.field[‘fieldname’].choices = new choices and then return form[‘fieldname’] but it doesn’t update
  • m

    mysterious-toddler-20573

    10/07/2022, 1:39 PM
    polling is surprisingly effective. For a long time browsers implemented SSE as polling under the covers. Not sure what's happened since then.
  • v

    victorious-cat-12942

    10/07/2022, 1:51 PM
    Does anyone know how to access a value on a get from an Django form select and access the hx-include value
  • g

    green-activity-6102

    10/07/2022, 3:53 PM
    it's been a long time since I did this so I might be missing some details somewhere, I recommend reading the docs. Notably the docs also say you can pass a callable to the
    choices
    argument instead of a static list, that might be helpful for you https://docs.djangoproject.com/en/4.1/ref/forms/fields/#django.forms.ChoiceField.choices
  • g

    green-activity-6102

    10/07/2022, 3:53 PM
    should just be in your
    request.GET
    query dict
  • v

    victorious-cat-12942

    10/07/2022, 4:31 PM
    Thanks I got it now
  • a

    aloof-painter-18008

    10/13/2022, 8:40 AM
    Hey guys. Love to be a part of the community. I'm new here and also with HTMX + Django. 1. How do you structure your interactive partial URLs? Right now my strategy is to build the UI logic with Alpine and have a common form which is hidden and bind the values to that hidden form whenever something is changed and HTMX will take care of the partial rendering of results. Works smoothly for me but I wanted to know if there is a better way. 2. Is it a good idea to move the UI logic to the backend using HTMX by just keep swapping the UI elements if required?
  • m

    mysterious-toddler-20573

    10/13/2022, 1:18 PM
    Hi Waseem, welcome! I'm not a django developer, but here is my take on those two questions: 1). With my htmx apps I tend to structure the URLs around the UI needs of the application, beyond the first level. So, I'll have the standard
    /contacts/
    ,
    /contacts/34
    , etc, but then I'll start getting things like
    /contacts/34/details
    for a details div that needs to poll or whatever. A bit different than how a JSON API would work. 2) In general, the more you can move to the backend, the better. If you have a lot of small changes that need to occur in the UI, then you probably want to expand the target of your HTTP requests to be as large as possible. If that won't work, there are other techniques available: https://htmx.org/examples/update-other-content/, but I always advocate keeping it as simple as possible
  • a

    aloof-painter-18008

    10/13/2022, 2:10 PM
    @mysterious-toddler-20573 That's a detailed answer. Thank you! 🙂
  • w

    white-motorcycle-95262

    10/13/2022, 9:19 PM
    I've been using
    django-compressor
    for awhile to compress js and css...but now I'm wondering: is there really a point to compressing/minifying multiple already-minified files into a single file? or is it kind of diminishing returns at that point? I know this is kind of more Django than HTMX, but figured people here would have good insight
  • h

    hundreds-camera-24900

    10/13/2022, 9:30 PM
    yeah fewer http requests
1...757677...100Latest