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

    astonishing-kilobyte-60820

    07/04/2022, 1:32 AM
    Can add this to the request and it should work
    Copy code
    setattr(request, "session", "session")
       messages = FallbackStorage(request)
       setattr(request, "_messages", messages)
  • a

    astonishing-kilobyte-60820

    07/04/2022, 1:34 AM
    I created a simple package to simplify the usage of htmx with django, check it out if you have a chance and let me know if you think there could be improvements or if there are any bugs! Thanks! https://pypi.org/project/simple-django-htmx/
  • g

    gray-morning-3453

    07/05/2022, 4:00 AM
    Hello all. I was in hyperscript room with my question earlier. But I think since my setup is django (pythonanywhere), perhaps I should ask around here. The problem is that in my setup HTMX is working quite fine except for hx-vals attribute. And if I try to use the same code in a standalone page, the get request is called just as expected. The code is this:
    Copy code
    <div id="calendar"
      hx-get="/day/"
      hx-vals="js:{date: new Date()}"
      hx-target="this"
      hx-swap="outerHTML"
      hx-trigger="load">
    </div>
    The get requested is initiated on the client side but without the ``date``querystring parameter appended to it. The same code works on a standalone local page.
  • g

    gray-morning-3453

    07/05/2022, 4:06 AM
    Solved! I was using an earlier version of HTMX library. Upon using the latest one, it works now.
  • g

    gray-morning-3453

    07/05/2022, 6:34 AM
    is this ready for production?
  • g

    gray-morning-3453

    07/05/2022, 6:38 AM
    Also how does this compare to django-htmx library? Can you share a brief overview?
  • a

    astonishing-kilobyte-60820

    07/05/2022, 11:39 PM
    It's brand new so can't be positive that it covers all edge cases, I am currently using it for a project of my own.
  • a

    astonishing-kilobyte-60820

    07/05/2022, 11:45 PM
    The only comparison to django-htmx is pretty much the name. There are really 2 main points to the library : 1) To avoid the messy views you get when you use htmx with django. With my library the htmx requests manage themselves meaning that you don't need to do
    if htmx reuqet .... else render regular view
    , see the documentation for more details on how that works. 2) To avoid all the extra urls! You don't need a new endpoint for every single request (edit, delete etc.) instead the htmx requests handle it themselves. (again see documentation for more details) It also has some built in htmx requests for basic functionality. If you want a delete button, there's a built in htmx request that all you do is pass in the object (to a template tag) that you want to delete and the built in htmx request will handle it for you! Please take a look at the repo and documentation. https://github.com/yaakovLowenstein/simple-django-htmx I am open to pull requests if there's anything you can think of adding/ or if any bugs are found! Thanks!
  • g

    gray-morning-3453

    07/06/2022, 7:29 AM
    Thanks! I ll read up the docs
  • g

    gray-morning-3453

    07/06/2022, 7:44 AM
    @astonishing-kilobyte-60820 In the name ``HtmxVIewMixin`` is capital I a deliberate choice?
  • a

    astonishing-kilobyte-60820

    07/07/2022, 12:58 AM
    nope , that's embarrassing πŸ™ˆ . Thanks for pointing it out. I will fix it shortly
  • a

    astonishing-kilobyte-60820

    07/07/2022, 12:59 AM
    What did you think. Any thoughts about it?
  • g

    gray-morning-3453

    07/07/2022, 4:44 AM
    (I am not an advanced-level programmer so I won't be able to give very great insights). I like it. Yes. I like the idea of hitting just one view for detail/list as well as Post/Get requests. The code will be neater and I feel it will bring clarity to the whole design. I am going to definitely use it in my project.
  • g

    gray-morning-3453

    07/07/2022, 5:46 PM
    @astonishing-kilobyte-60820 Can you show a full example of how this can be implemented?
  • g

    green-napkin-82002

    07/07/2022, 11:50 PM
    Hello everyone, I wrote my first article on handling full page reloads with Django and htmx and would love some feedback! https://medium.com/@john1galiszewski/how-to-handle-full-page-reloads-with-django-and-htmx-a9a7ef2e4b1c
    b
    g
    • 3
    • 12
  • b

    blue-ghost-19146

    07/08/2022, 3:56 AM
    Django and htmx article
  • h

    handsome-shampoo-48908

    07/08/2022, 8:46 AM
    Hello, I am a new user of htmx. So I can't find how to do what I want. On a simple task list, when I create a new task collection, I want to redirect to the new collection list. If I do the redirect in my view, the html is loaded into the collection. In the screenshot below I have my default collection and when I create my test collection it is added but I would like to be directly redirected to this collection.
  • h

    handsome-shampoo-48908

    07/08/2022, 8:47 AM
    I use slug like this : http://127.0.0.1:8000/?collection=_default
  • h

    handsome-shampoo-48908

    07/08/2022, 8:47 AM
    Thanks in advance for your help πŸ˜‰
  • h

    handsome-shampoo-48908

    07/08/2022, 8:58 AM
    html:
    Copy code
    html
                    <form method="POST" class="d-flex gap-2 mt-2">
                        {% csrf_token %}
                        <input type="text"
                               name="collection-name"
                               class="form-control"
                               placeholder="Enter a new collection ...">
                        <button class="btn btn-success"
                                hx-post="{% url 'add-collection' %}"
                                hx-target="#collections"
                                hx-swap="beforeend"
                                type="submit">Add
                        </button>
                    </form>
    views:
    Copy code
    py
    
    def add_collection(request):
        collection_name = escape(request.POST.get("collection-name"))
        collection, created = Collection.objects.get_or_create(name=collection_name, slug=slugify(collection_name))
        if not created:
            return HttpResponse("The collection already exists.", status=409)
    
        return render(request, 'tasks/collection.html', context={"collection": collection})
  • r

    ripe-action-67367

    07/08/2022, 9:34 AM
    2 options: 1. HX-redirect header to send user to another page https://htmx.org/reference/#response_headers. 2. Use OOB swaps to update both "Collections" and "Tasks" in combination with HX-Push header
  • h

    handsome-shampoo-48908

    07/08/2022, 9:41 AM
    Thanks for your feedback, i have see these options but i haven't understand how they work. i will go read this part of the documentation again. πŸ˜‰
  • h

    handsome-shampoo-48908

    07/08/2022, 9:50 AM
    Yeah i have understand the HX-redirect ! Thanks again ! Have a good day.
  • a

    ambitious-london-57076

    07/08/2022, 11:18 AM
    Hi everyone, Still trying to improve at this. I have an issue with loading a button that simply toggles the state of a post whether it is published or unpublished. My Django view function works well and edits the Post database item. The function call seems to trigger the function but for some reason, the button does not update appearance. Instead the button disappears. Once I reload the page, the button appearance is as it should be. Any ideas?
  • s

    silly-bear-76516

    07/08/2022, 2:23 PM
    Does the
    admin-posts-partial
    have those buttons? You can always inspect using webtools the html that is rendered by the backend on response to the toggle
  • a

    ambitious-london-57076

    07/09/2022, 5:47 AM
    İt seems like my mistake was, I was trying to reload the section without using a partial at all. 🫒 Finally fixed. Thank you @silly-bear-76516
  • b

    blue-ghost-19146

    07/09/2022, 2:24 PM
    Can anyone explain why PUT requests need a csrf token added with hx-headers but POST requests don’t (from my experience so far)? I was under the impression that all htmx requests except GET required this to avoid 403 Forbidden
  • b

    brave-processor-48373

    07/09/2022, 3:01 PM
    I think they do, unless you’re attaching the token to the form using {% csfr_token %}
  • b

    blue-ghost-19146

    07/09/2022, 3:14 PM
    Yeah, attaching the token in the tag like that works for POST but not for PUT. In PUT’s case, only adding the token to the htmx request (hx-headers or requestConfig) works. Just unsure why!
  • b

    brave-processor-48373

    07/09/2022, 3:28 PM
    Ah i see, hadn’t realized that tbh!
1...636465...100Latest