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

    lively-traffic-17360

    01/06/2022, 6:01 PM
    the hx-delete is here
  • l

    lively-traffic-17360

    01/06/2022, 6:02 PM
    he delete the data, but do not reload the list
  • l

    lively-traffic-17360

    01/06/2022, 6:03 PM
    my trigger for reload is hx-list-updated
  • p

    plain-kangaroo-26043

    01/06/2022, 6:03 PM
    Another approach re modals is have the hx-target the modal container, on error send back the modal content into the target, on success send an empty HttpResponse. If you need full reload you can always send back Hx-Refresh header.
  • l

    lively-traffic-17360

    01/06/2022, 6:03 PM
    but don't work
  • l

    lively-traffic-17360

    01/06/2022, 6:07 PM
    hmm, I will try this, thanks
  • f

    fresh-controller-36545

    01/06/2022, 6:13 PM
    Mmm how do you deal with removing the overlaying modal-background though when just sending back an empty response?
  • p

    plain-kangaroo-26043

    01/06/2022, 6:14 PM
    The modal container is just an empty div, which holds the modal overlay+modal.
  • f

    fresh-controller-36545

    01/06/2022, 6:14 PM
    Don't think that was meant for you. Maybe try changing it to
    HX-Trigger-After-Settle
    ? Honestly unsure why it doesn't work.
  • p

    plain-kangaroo-26043

    01/06/2022, 6:16 PM
    but yeah, was talking of a general technique, not sure if it would work for @User
  • f

    fresh-controller-36545

    01/06/2022, 6:17 PM
    Gotcha. Well I guess it depends on how you want to model your
    fragments
    /
    partials
    (really gotta norm that term)
  • f

    fresh-controller-36545

    01/06/2022, 6:18 PM
    Like if you want to use the same modal to display multiple different forms, it might make sense to just have a modal as a div on the page itself and just fill in a
    form partial
  • l

    lively-traffic-17360

    01/06/2022, 6:18 PM
    no problem, the answer gave me a idea, tried hx-refresh but do not work again
  • l

    lively-traffic-17360

    01/06/2022, 6:20 PM
    hmm.. can be a solution
  • l

    lively-traffic-17360

    01/06/2022, 7:34 PM
    guys, the error was in return from my view, thanks for help
  • w

    white-motorcycle-95262

    01/07/2022, 12:31 AM
    Anyone know if adamchainz (from django-htmx) is in the discord? I put together some class based views/mixins to facilitate HTMX and I'm curious to know if there's any interest in adding them to the repo.
  • h

    hundreds-camera-24900

    01/07/2022, 12:34 AM
    he is but not active I think
  • f

    fresh-controller-36545

    01/07/2022, 12:34 AM
    Just drop them. I'll start in the spirit of exchange
    Copy code
    python
    class HtmxTemplateResponseMixin(TemplateResponseMixin):
    
        htmx_template_name: str = None
    
        def get_template_names(self):
            """
            Return a list of template names to be used for the request. Must return
            a list. May not be called if render_to_response() is overridden.
            """
            super().get_template_names()
    
            if self.template_name is None or self.htmx_template_name is None:
                raise ImproperlyConfigured(
                    "HtmxTemplateResponseMixin requires either a definition of "
                    "'htmx_template_name' and 'template_name' or an "
                    "implementation of 'get_template_names()'"
                )
    
            if self.request.htmx and not self.request.htmx.boosted:
                return [self.htmx_template_name]
    
            return [self.template_name]
  • w

    white-motorcycle-95262

    01/07/2022, 12:36 AM
    This is similar to what I did, except the class has a
    template_names
    dict, where
    default
    key goes to the full template, and then keys for each id go to template snippets for only updating portions of the page.
  • f

    fresh-controller-36545

    01/07/2022, 12:36 AM
    Also a good idea. I just don't like coupling front- and backend that tightly
  • w

    white-motorcycle-95262

    01/07/2022, 12:40 AM
    Hmm, fair enough. I also have a FormView:
    Copy code
    class HtmxFormView(HtmxTemplateResponseMixin, FormView):
        """Extend Django's FormView for HTMX."""
    
        def form_invalid(self, form) -> HttpResponse:
          """Returns form template with errors."""
          if 'form' not in self.template_names:
            message = (
              "HtmxFormView requires a `form` value for `template_names`."
            )
            raise ImproperlyConfigured(message)
    
          response = render(
            self.request,
            template_name=self.template_names['form'],
            context=self.get_context_data(),
          )
          response['HX-Retarget'] = self.request.htmx.trigger
          return response
    But in my situation all the forms are fetched by HTMX and only one form is displayed at a time.
  • m

    mysterious-toddler-20573

    01/07/2022, 12:40 AM
    @User ☝️
  • w

    white-motorcycle-95262

    01/07/2022, 12:40 AM
    form_valid
    would be need set on each view that subclasses
    HtmxFormView
    , since that's a bit specific
  • w

    white-motorcycle-95262

    01/07/2022, 12:45 AM
    Here's my
    HtmxTemplateResponseMixin
    Copy code
    class HtmxTemplateResponseMixin(TemplateResponseMixin):
        """Extend Django's Class Based Viws for HTMX."""
    
        template_names = None
    
        def get_template_names(self) -> list:
          """Fetches proper template depending on request."""
    
          if self.template_names is None:
            message = (
              "HtmxTemplateResponseMixin requires either a definiton of "
              "the `template_names` dictionary or an implementation of "
              "`get_template_names()`."
            )
            raise ImproperlyConfigured(message)
    
          try:
            template_name = self.template_names['default']
          except KeyError:
            message = (
              "HtmxTemplateResponseMixing requires that the `template_names` "
              "dictionary have a value for `default` or an implementation of "
              "`get_template_names()`."
            )
            raise ImproperlyConfigured(message)
    
          if self.request.htmx:
            try:
              template_name = self.template_names[self.request.htmx.target]
            except KeyError:
              pass
          return [template_name]
    This can be used to get the following CBV:
    Copy code
    class HtmxTemplateView(HtmxTemplateResponseMixin, TemplateView):
        """Extend Django's TemplateView for HTMX."""
        pass
    
    
      class HtmxDetailView(HtmxTemplateResponseMixin, DetailView):
        """Extend Django's TemplateView for HTMX."""
        pass
    
    
      class HtmxListView(HtmxTemplateResponseMixin, ListView):
        """Extend Django's ListView for HTMX."""
        pass
  • f

    fresh-controller-36545

    01/07/2022, 12:46 AM
    I always wondered how people handle a denormalized data-structure that require multiple
    forms
    to edit with a single CBV when using some sort of form mixin. I know somewhat unrelated, but…
  • f

    fresh-controller-36545

    01/07/2022, 12:47 AM
    Looks good. Thanks for sharing. Always curious to see what other people are coming up with. CBVs would definitely be a good addition to
    django-htmx
    – though the community is somewhat emotionally split between
    explicit
    and
    implicit
    views/apis.
  • f

    fresh-controller-36545

    01/07/2022, 12:50 AM
    Will probably adapt mine to optionally take a dict as the
    htmx_template_name
    – quite like it actually.
  • w

    white-motorcycle-95262

    01/07/2022, 12:52 AM
    I suppose I should mention that another "trick" I use is that my base template is something like this:
    Copy code
    {% if not request.htmx %}
    <head>[head stuff, meta, etc, load css]</head>
    <body>
    <nav> [ navbar stuff that's on every page] </nav>
    <main id="content">
    {% endif %}
    [ content ]  
    {% if not request.htmx %}
    </main>
    <footer> [footer info that's on every page] </footer>
    {% block footer_js %}[load js libs]{% endblock %}
    {% endif %}
  • f

    fresh-controller-36545

    01/07/2022, 12:55 AM
    To make responses smaller?
  • w

    white-motorcycle-95262

    01/07/2022, 12:57 AM
    Well, you need to load css/js etc when the page is being accessed for the first time (let's say someone goes directly to example.com/about/), but once all that has been loaded once if you're using
    hx-boost
    there's no need to reload them, so yeah. (although, I suppose browsers cache css and js libs and such, but I'm not 100% on how that works).
1...373839...100Latest