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

    hundreds-camera-24900

    10/29/2021, 9:30 PM
    it looked like you rolled your own header detection
  • w

    white-magazine-23424

    10/29/2021, 9:31 PM
    right now, my library is more of a half-baked idea with only ~200 total lines of code, so the extent of my header detection is this:
    Copy code
    python
    def is_hx_request(request):
        return request.META.get("HTTP_HX_REQUEST") == "true"
    so, I'm not really breaking a sweat there πŸ˜†
  • w

    white-magazine-23424

    10/29/2021, 9:32 PM
    the django-htmx library integrates django and htmx, I'm hoping my library can integrate django-rest-framework and htmx in a way that doesn't step on the toes of django-htmx, and also doesn't depend on django-htmx
  • w

    white-magazine-23424

    10/29/2021, 9:33 PM
    Ideally, users can install one or the other or both depending on their needs, but for now I'm just not listing django-htmx as a dependency until I need to for some reason
  • h

    hundreds-camera-24900

    10/29/2021, 9:36 PM
    I've done a lot of drf work, but It's always been w/ json
  • h

    hundreds-camera-24900

    10/29/2021, 9:36 PM
    if you render a form to a user and expect them to submit to the endpoint
  • h

    hundreds-camera-24900

    10/29/2021, 9:37 PM
    would they use formenc or json?
  • w

    white-magazine-23424

    10/29/2021, 9:38 PM
    DRF has all that content-negotiation already built-in. So, you know how when you are in the browsable api, it will show you a little form that you can fill out and submit? That form is sending data as formenc to the same endpoint as the one you are writing for json; the endpoint already can handle both
  • h

    hundreds-camera-24900

    10/29/2021, 9:39 PM
    for some reason I thought that form was converting to json and ajaxing it in
  • h

    hundreds-camera-24900

    10/29/2021, 9:39 PM
    I guess the only wierdish thing there then would be if you use a form or a serializer?
  • h

    hundreds-camera-24900

    10/29/2021, 9:40 PM
    forms could theoretically autogenerate the html but I don't normally do that anyway
  • w

    white-magazine-23424

    10/29/2021, 9:42 PM
    Again, drf already handles it! When you pass
    template_name
    as a kwarg to the
    Response
    constructor, drf passes
    data
    (first argument to the constructor) as context to the template behind the scenes if the request if for the content type of
    text/html
    So:
    Copy code
    python
    @api_view
    def foo_view(request):
      return Response({'message': 'hello!'}, template_name="foo.html")
    Copy code
    html
    <h1>{{ messgae }}</h1>
  • w

    white-magazine-23424

    10/29/2021, 9:43 PM
    If you make a request to that view with a
    Accept: application/json
    header, it will return
    b'{"message": "hello!"}'
    , but if you send a request to the same view with
    Accept: text/html
    , it will return
    b'<h1>hello!</h1>'
  • w

    white-magazine-23424

    10/29/2021, 9:44 PM
    The only caveat being that by default, the renderer for
    text/html
    is the BrowsableAPIRenderer, so actually by default, it would give the whole browsable api UI for
    text/html
    requests, but that behavior can be easily changed with settings
  • b

    bitter-dog-98292

    10/29/2021, 11:47 PM
    Hi I am trying to render a list again using the solution 3 here : https://htmx.org/examples/update-other-content/ It’s nearly working but after adding a new item to the list, there is a new ul inside the previous ul. Does someone have a working example with Django ? Thanks a lot in advance
  • m

    mysterious-toddler-20573

    10/30/2021, 12:13 AM
    maybe you need to use an
    outerHTML
    swap?
  • b

    bitter-dog-98292

    10/30/2021, 7:41 AM
    @User thanks but no it does not work. Basically, I am including the list like: {% include "account/addresses/addresses_list.html" %} Which looks like this: <ul id="addresses-list" hx-get="{% url 'account_addresses_list_hx' %}" hx-trigger="newAddress from:body" hw-swap="outerHTML" role="list" class="grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-3"> {% for address in addresses %} {% include "account/addresses/address.html" with address=address %} {% endfor %} And that view does: def account_addresses_list_hx(request): context = {} addresses = Address.objects.filter(user=request.user) context["addresses"] = addresses return render(request, "account/addresses/addresses_list.html", context) So nothing really complex. What am I getting wrong ? Thanks a lot in advance.
  • b

    breezy-army-85649

    10/30/2021, 8:14 AM
    I think the problem is just that you wrote hw-swap instead of hx-swap.
  • b

    bitter-dog-98292

    10/30/2021, 3:00 PM
    @User Indeed, I just need a new pair of glasses. Thanks a lot !
  • g

    gorgeous-airport-54386

    10/30/2021, 4:36 PM
    Was this talk recorded anywhere?
  • f

    fresh-controller-36545

    10/30/2021, 7:24 PM
    Interesting. Did you btw explore how viable it is to use a DRF serializer as a form-serializer?
  • w

    white-magazine-23424

    10/30/2021, 8:08 PM
    @User yeah, DRF serializers already have a lot of support for rendering forms: https://www.django-rest-framework.org/topics/html-and-forms/
  • w

    white-magazine-23424

    10/30/2021, 8:12 PM
    this is why the amount of work for a htmx + rest framework library is not really much. The only thing I'd hope to see is a content negotiator that is responsive to the
    Hx-Request
    header (i.e., it returns a partial page for htmx and something else for normal requests), and a revamp of the browsable renderer where the something else returned for normal requests is a UI component explorer thing (alongside a BrowsableAPI data view). Everything else in terms of content negotiation, rendering, and the API a library user would interact with is already there in DRF. So, my vision for the library is just to drop-in a content-negotiator class and a render class that is a UI component explorer; quite minimal
  • c

    calm-mouse-38665

    10/31/2021, 12:58 PM
    My question may have been asked before, and if so I apologise. I am using django-tailwind package (which is awesome for minimizing css without scss). I have used htmx for dynamic reactive forms, and alpinejs for "vue-like" ui/ux. My question is: are htmx and alpinejs mutually exclusive? Has anyone done a matrix of where they overlap and when to use one or the other? Will developers in django always use both? I would prefer to use htmx only, to simplify the code, but also think that if it would compromise the ui/ux experience then I'll stick with both.
  • p

    powerful-ambulance-40990

    10/31/2021, 1:30 PM
    Hello, there is a spanish video on youtube:

    https://www.youtube.com/watch?v=dEg-K3kMj60β–Ύ

    , source: https://twitter.com/hernantz/status/1453106733669363712?s=20 , other links: https://twitter.com/hernantz, http://hernantz.github.io/, https://github.com/hernantz/django-htmx-demo.
  • f

    fresh-controller-36545

    10/31/2021, 2:35 PM
    No they're not – they're complementary. HTMX simply handles AJAX requests and Alpine.js enhances frontend UX.
  • c

    calm-mouse-38665

    10/31/2021, 2:39 PM
    Thank you for your response, this is what I thought without tl;dr; research. πŸ˜‚
  • b

    best-arm-55473

    11/04/2021, 4:11 AM
    Using tabs (described in htmx examples), I’ve loaded fetched content and it works great! However what doesn’t seem to carry over is the running of custom js module to process the fetched content -
  • b

    best-arm-55473

    11/04/2021, 4:14 AM
    Before the change to tabs, I loaded everything into a template file and at the end, the
    extra_js
    block looked like this:
    Copy code
    {% block extra_js %}<script type="module" src="{{tree_populator_js}}"></script>{% endblock extra_js %}
    A DOM node (filtered with
    |json_script
    ) is passed to a function of
    tree_populator_js
    . With the tab pattern, can't seem to call the said function anymore. Hoping for tips in the right direction
  • s

    salmon-xylophone-28580

    11/04/2021, 7:20 AM
    Maybe this helps: https://htmx.org/api/#process
1...111213...100Latest