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

    bland-coat-6833

    02/26/2023, 11:58 AM
    You’ll always get something in that header though I think. So you’d have to be careful you don’t try and get a fragment that you don’t want or that isn’t there. Probably better to be explicit rather than implicit (where have I heard that before πŸ€”πŸ˜‚)
  • b

    billowy-insurance-11390

    02/27/2023, 1:31 PM
    Hi everyone, hope you are having a great day. In my Django project, I am trying htmx. I was able to log in and redirect to the home page without the page reloading. But now I need my navbar to be updated accordingly as well. Here is my base.html HTMX Blog <script src="https://unpkg.com/htmx.org@1.8.5%22%3E {% include 'navbar.html' %} {% block body %} {% endblock %} Here is my login form submit button. <button class="btn btn-success" hx-post="{% url 'login' %}" hx-push-url="true" hx-target="#content" hx-swap="innerHTML" hx-indicator="#content-loader" type="submit">Login Here is my navbar.html {% if user.is_authenticated %} Logout {% else %} Login {% endif %} Now, when I log in I still see the Login even though I am authenticated. I understand that I don't see the authenticated part since the navbar is not updated from the response. How can I update the navbar as well accordingly?
  • e

    echoing-lion-55988

    02/27/2023, 3:57 PM
    I had very similar issue with my application, which is not written in Django. My solution was to just use good old and return new HTML to be loaded by the browser. I’ve tried dealing with events etc but just reloading the page was the simplest solution, so I’ve ended up writing less code.
  • b

    bitter-monkey-50309

    02/27/2023, 7:24 PM
    Take a look at the boost attribute (https://htmx.org/attributes/hx-boost/) or consider also returning your nav bar HTML using an Out of Band swap (https://htmx.org/attributes/hx-swap-oob/)
  • b

    billowy-insurance-11390

    02/28/2023, 9:45 AM
    Thanks Out of Band swap solved the issue.
  • e

    echoing-lion-55988

    02/28/2023, 2:46 PM
    wow, I didn't know about oob
  • e

    echoing-lion-55988

    02/28/2023, 2:47 PM
    it looks really cool, but I needed some time to understand how it works
  • m

    melodic-sugar-39315

    02/28/2023, 7:34 PM
    Hello, somebody know a tutorial for build a server sent event SSE, with django channels and htmx????
  • e

    eager-psychiatrist-68229

    03/02/2023, 8:24 AM
    django isn't really suited for long polling, next best thing is websocket

    https://www.youtube.com/watch?v=FcVwDEcu6K0β–Ύ

  • m

    melodic-sugar-39315

    03/02/2023, 9:35 PM
    Hi thanks for your answers. How i can return a custom headers in a consumer response?
  • e

    eager-psychiatrist-68229

    03/03/2023, 8:04 AM
    depends, either in the view or in a custom middleware if it needs to be widespread
  • e

    eager-psychiatrist-68229

    03/03/2023, 8:06 AM
    django-htmx has a function to help send client events
  • a

    alert-grass-16919

    03/06/2023, 11:37 AM
    Here's my problem: I'm loading a complex form in a modal via htmx,. In this form I'm using vanilla js to dynamically add/remove formset. Everything works on first try but when I load the modal another time via htmx I have errors because the variables in my custom script are already declared. How can I solve that?
    Copy code
    <script>    let ingredientForm = document.querySelectorAll(".ingredient-form")    let ingredientFormList = document.querySelector('#ingredient-form-list')    let container = document.querySelector("#form-container")    let addButton = document.querySelector("#add-form")    let deleteButton = document.querySelector("#delete-form")    let totalForms = document.querySelector("#id_form-TOTAL_FORMS")    let formNum = ingredientForm.length-1 // Get the number of the last form on the page with zero-based indexing
    the error is
    Copy code
    VM13:1 Uncaught SyntaxError: Identifier 'ingredientForm' has already been declared
    I solved the problem by forcing a full page refresh when form is saved (not ideal as I want only to refresh a partial) but the problem remains if I close the modal an open it again...
  • l

    late-carpet-36627

    03/06/2023, 4:24 PM
    hey guys, I'm trying to use htmx.on (https://htmx.org/api/#on) to intercept an HX-Trigger from a POST response header, but it doesn't seem to be working. Does htmx.on look at all response headers, even if they weren't made from an hx-get/hx-post?
  • m

    mysterious-toddler-20573

    03/06/2023, 4:55 PM
    no, has to be through htmx
  • l

    late-carpet-36627

    03/06/2023, 5:01 PM
    thanks πŸ™‚ headache solved
  • r

    refined-manchester-67193

    03/06/2023, 10:27 PM
    I'm testing a Django view which also responds to HTMX requests, when I hit that line however I get this error:
    Copy code
    >       if request.htmx:
    E       AttributeError: 'WSGIRequest' object has no attribute 'htmx'
  • l

    late-carpet-36627

    03/06/2023, 10:37 PM
    Do you have the django-htmx package installed? request.htmx doesn't work unless you have that package
  • r

    refined-manchester-67193

    03/06/2023, 10:37 PM
    yes I do
  • r

    refined-manchester-67193

    03/06/2023, 10:38 PM
    request.htmx is working, it's specifically when I run tests
  • l

    late-carpet-36627

    03/06/2023, 10:38 PM
    oh gotcha, idk then
  • b

    bitter-monkey-50309

    03/06/2023, 10:57 PM
    Sounds like the middleware isn't installed? Do you have copies of your settings, one for running and one for testing? Make sure it's in both
  • r

    refined-manchester-67193

    03/06/2023, 11:01 PM
    It is there
  • r

    refined-manchester-67193

    03/06/2023, 11:01 PM
    In the
    base_settings.py
    file, which is common to
    dev.py
    and
    prod.py
  • b

    bitter-monkey-50309

    03/06/2023, 11:06 PM
    Is this a unit test that's failing? Are you using the test client or
    RequestFactory
    ?
  • f

    flaky-fish-46395

    03/08/2023, 8:29 PM
    Hey guys, a lil bit off top, but maybe you know why 'docker compose up' ignores '.dockerignore' file and copies everything into a container?
  • r

    refined-manchester-67193

    03/08/2023, 8:29 PM
    @bitter-monkey-50309 I am using
    RequestFactory
    , here's how I solved it for now:
    Copy code
    python
    request_factory = RequestFactory()
    
        request = request_factory.get(reverse("s"))
        request.user = profile.user
        request.htmx = True
  • s

    shy-zebra-22292

    03/09/2023, 6:12 AM
    generally the problem is that you're setting the variables in the global scope. In the main window object that is one way to go around that would be to put that js in a self-invoking function . (see more here https://linuxhint.com/self-invoking-functions-in-javascript/#:~:text=In%20JavaScript%2C%20a%20%E2%80%9CSelf%2D,by%20another%20set%20of%20parenthesis. ) something like
    Copy code
    (function () {
        // your code here
        let ingredientForm = document.querySelectorAll(".ingredient-form")    let ingredientFormList = document.querySelector('#ingredient-form-list')    let container = document.querySelector("#form-container")    let addButton = document.querySelector("#add-form")    let deleteButton = document.querySelector("#delete-form")    let totalForms = document.querySelector("#id_form-TOTAL_FORMS")    let formNum = ingredientForm.length-1 // Get the number of the last form on the page with zero-based indexing
       //end of your code
    })();
    this will not put your variables in the global (window) scope, so when you reload from HTML your function should run again without problem.
  • a

    alert-grass-16919

    03/09/2023, 9:24 AM
    Thanks for your help.. I solved declaring the variable using var instead of let and getting out of the global scope
  • s

    shy-zebra-22292

    03/09/2023, 4:17 PM
    Happy you found a solution
1...949596...100Latest