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

    gorgeous-airport-54386

    07/14/2021, 6:19 PM
    👀
  • e

    elegant-london-97728

    07/14/2021, 6:37 PM
    Fantastic. Let's django. Maybe we can even collate useful tips here and build them up into examples for a subchapter in the htmx docs.
  • e

    elegant-london-97728

    07/14/2021, 6:54 PM
    Anybody has a good example of using Django's Paginator for an htmx "Active Search"-like case?
  • u

    user

    07/14/2021, 6:56 PM
    Doesn't an Active Search defeats the purpose of having pages?
  • e

    elegant-london-97728

    07/14/2021, 7:02 PM
    Well say you search for a generic term but get like 50 results, it would be nice to split this up into a handful of pages.
  • e

    elegant-london-97728

    07/14/2021, 7:04 PM
    I've got it kind of working now by returning the pagination template in the html fragment (disclaimer: htmx newbie here).
  • u

    user

    07/14/2021, 7:10 PM
    That's what htmx is all about. HTML fragments. Not much mistery.
  • e

    elegant-london-97728

    07/14/2021, 7:15 PM
    Slowly getting there, my htmx-aware self was born only yesterday.
  • u

    user

    07/14/2021, 7:16 PM
    I'm not so "old" in htmx myself.
  • m

    mysterious-toddler-20573

    07/14/2021, 7:34 PM
    if there is a standard paging widget for django, you can probably slap an hx-boost on a div around it and it will "just work"
  • m

    mysterious-toddler-20573

    07/14/2021, 7:36 PM
    no one is 🙂
  • s

    salmon-xylophone-28580

    07/15/2021, 7:10 AM
    I am looking for feedback to my naming pattern (django function based views): https://github.com/guettli/django-htmx-fun#naming-pattern
  • e

    elegant-london-97728

    07/15/2021, 7:33 AM
    @User sounds good; personally: I'd omit
    _page
    as a suffix for full-page responses (treating this as a default: no suffix === full-page response, so existing codebases fit this pattern)
  • e

    elegant-london-97728

    07/15/2021, 7:35 AM
    You could also have
    _hx
    cover both GET and POST, with
    _hxget
    and
    _hxpost
    forcing one or the other.
  • e

    elegant-london-97728

    07/15/2021, 7:40 AM
    I'm also thinking about the best (sub)directory structure for everything (views, templates). I'll probably go for
    /hx/
    subdirectories, kind of like
    /partials/
    but for htmx.
  • e

    elegant-london-97728

    07/15/2021, 7:41 AM
    Do you have related conventions for CBVs?
  • s

    salmon-xylophone-28580

    07/15/2021, 7:42 AM
    I started like this too. But then I realized that I have few methods returning a full page. So I changed to the explicit "_page" suffix to distinguish between normal methods and full page responsed.
  • s

    salmon-xylophone-28580

    07/15/2021, 7:43 AM
    I don't use CBV any more.
  • e

    elegant-london-97728

    07/15/2021, 8:23 AM
    Any specific reasons? I myself started functional, then went more CBV, and am now doing a mix.
  • s

    salmon-xylophone-28580

    07/15/2021, 8:51 AM
    I started with FBV (12 years ago), then moved to CBV, but still often get confused by CBV. With htmx I write tiny methods returning small html fragments. FBV feel more convenient for me. Just my personal opinion.
  • m

    mysterious-toddler-20573

    07/15/2021, 1:38 PM
    the way I do it in rails is as follows:
    /views/contacts/index.html.erb
    - main template for the contacts controller root URL and if that has something like the active search pattern in it:
    Copy code
    html
    <h3> 
      Search Contacts 
      <span class="htmx-indicator"> 
        <img src="/img/bars.svg"/> Searching... 
       </span> 
    </h3>
    <input class="form-control" type="text" 
           name="search" placeholder="Begin Typing To Search Users..." 
           hx-post="/search" 
           hx-trigger="keyup changed delay:500ms" 
           hx-target="#search-results" 
           hx-indicator=".htmx-indicator">
    
    <table class="table">
        <thead>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Email</th>
        </tr>
        </thead>
        <tbody id="search-results">
        </tbody>
    </table>
    I'll use the ID of the element I am targeting for a template name:
    /views/contacts/_search_result.html.erb
  • m

    mysterious-toddler-20573

    07/15/2021, 1:38 PM
    i'd probably pick a different id, looking at it, like
    results_table
    or something, so I have a better file name
  • m

    mysterious-toddler-20573

    07/15/2021, 1:41 PM
    And then in the controller, the logic that handles the
    /search
    url would look something like this:
    Copy code
    rb
      if request.headers['HX-Request']
        render "_search_results.html.erb" # render the partial
      else 
        render "index.html.erb" # render the whole UI w/ search results
  • m

    mysterious-toddler-20573

    07/15/2021, 1:43 PM
    The final trick here being that the
    index.html.erb
    template will include the
    search_results.html.erb
    template so it can be used as the template for both the root request as well as
    /search
    requests
  • m

    mysterious-toddler-20573

    07/15/2021, 1:43 PM
    not sure if that's useful for django folks, but hat's how I usually do it
  • c

    clean-vegetable-65167

    07/15/2021, 1:54 PM
    Hi guys, another Django user here 🙂 I am wondering how to properly handle Django's generic UpdateView with htmx. The thing is django returns form with errors if the form sent is invalid which plays nicely, but it does return redirect on success, which then replaces the content by the redirect response. Is there a way to tell htmx to follow redirect if it arrives as a response? Thanks
  • m

    mysterious-toddler-20573

    07/15/2021, 1:56 PM
    htmx should automatically follow redirects: the ajax request never sees it, rather it just sees the new content
  • m

    mysterious-toddler-20573

    07/15/2021, 1:56 PM
    with htmx you usually do not need to do a redirect however, since the request is AJAX and therefore won't be in the browser history and cause "resubmit on refresh" issues like normal web requests
  • m

    mysterious-toddler-20573

    07/15/2021, 1:57 PM
    so, if there is a way to simply render the updated content after a successful save without a redirect, that should work too
  • s

    salmon-xylophone-28580

    07/15/2021, 2:04 PM
    A normal 302 gets evaluated by htmx like "get this snippet from the URL you find in the "location" header". If you want htmx to do full page reload you need to set the header "hx-redirect". See: https://stackoverflow.com/questions/65569673/htmx-hx-target-swap-html-vs-full-page-reload
12345...100Latest