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

    bitter-monkey-50309

    10/27/2022, 11:23 AM
    Why do you want to avoid the redirect? You could do
    HX-Redirect
    or
    HX-Location
    (which isn't technically a redirect). https://htmx.org/reference/#response_headers
  • r

    red-france-69804

    10/27/2022, 11:41 AM
    I want to avoid redirections for 2 reasons: 1) Using a redirect reloads the whole page and is slower than loading the required fragment only. To me, this speed / "spa-like feeling" is the whole interest of HTMX. 2) Because it would break my custom toast message system. A full HTTP redirection will remove the current displayed messages in the DOM. I want to keep the ability to navigate between "pages" without erase the current messages (like a SPA). To not break my toast system, I need to ensure a part of the DOM is never removed. However, I'm curious about how the
    HX-Location
    could (or not) fix my problem.
  • r

    red-france-69804

    10/27/2022, 11:44 AM
    Thanks. I thought of this one, but I think that trying to use a "all-in-one" view a very complex, and not very scalable. I think a view should keep dedicated to do a single action.
  • g

    green-activity-6102

    10/27/2022, 2:34 PM
    maybe on the form response HTML you could attach an
    hx-trigger="load"
    to immediately go and fetch the detail view HTML
  • b

    blue-gold-89534

    10/27/2022, 5:35 PM
    I rewrote my download to the following:
    Copy code
    python
    def download(request, pk):
        if request.htmx:
            link = f"""download/{pk}" """
            tmp = f"""hx-trigger="load" hx-redirect="{static(link)}" """
            return HttpResponse(format_html(f"""<img {tmp} src="{static('images/startl.svg')}">"""))
        else:
            return FileResponse(open() ...)
    I hoped I can directly load an Htmx indication ICON, and put a redirect on the file on it ... not soo smart?
  • b

    blue-gold-89534

    10/27/2022, 5:51 PM
    but the load is never being fired
  • l

    lemon-rainbow-71647

    10/27/2022, 6:16 PM
    Did you miss a " in link-f-string?
  • g

    green-activity-6102

    10/27/2022, 8:40 PM
    also why are you using those triple quotes... makes readability super tough
  • b

    blue-gold-89534

    10/28/2022, 9:58 AM
    I kind of like the """ fstring annoation
  • b

    blue-gold-89534

    10/28/2022, 10:00 AM
    Copy code
    html
    <img hx-trigger="load" hx-redirect="/static/download/880" src="/static/images/startl.svg" class="">
    this is what I can see in the webdev console after the click, which seems correct. But it is not fired
  • b

    blue-gold-89534

    10/28/2022, 10:00 AM
    is this because the swapped in "load" attribute is not being fired?
  • b

    blue-gold-89534

    10/28/2022, 10:00 AM
    I kind of hoped it would fire right away
  • b

    bitter-monkey-50309

    10/28/2022, 12:45 PM
    What are you trying to do?
    hx-redirect
    isn't an attribute you can use in your HTML it's a header you can set on the response. You'll also need to swap
    src
    to
    hx-get
    for the
    load
    trigger to do anything, otherwise your image is already loaded
  • g

    green-activity-6102

    10/28/2022, 4:40 PM
    you dont need to use triple quotes to use f-strings
  • f

    few-vegetable-15970

    10/28/2022, 6:19 PM
    Yes, I'm often using this technique. It's two successive GET 200. This first time with a simple div with the hx-get and the load trigger. Works very well.
  • f

    few-vegetable-15970

    10/28/2022, 7:05 PM
    It's not a beginner question at this stage. I see your point, someone in #939530480636809227 channel ask something very similar about avoiding the redirection. You could extract the logic I described before in a mixin, and use
    fragment_name
    class attribute to know what to render in
    form_valid
    .
  • r

    red-dinner-66239

    10/30/2022, 10:31 AM
    Hello, I have a small question: is it possible that my page refreshes only when I receive a POST in my django views?
  • r

    red-dinner-66239

    10/30/2022, 10:55 AM
    there is no way for example to send a POST with my views to activate a refresh in my html with the htmx?
  • b

    big-engine-22348

    10/30/2022, 11:37 AM
    if you DONT use htmx it will refresh the page
  • b

    big-engine-22348

    10/30/2022, 11:38 AM
    but the point of htmx is that it will not reload the complete website
  • r

    red-dinner-66239

    10/30/2022, 11:39 AM
    yes but in fact it's because my data has each a popup and it closes 5s later seen that the page refreshes
  • b

    boundless-vase-80440

    10/30/2022, 12:00 PM
    Don't refresh the whole page, maybe?
  • r

    red-dinner-66239

    10/30/2022, 12:01 PM
    I only refresh a section where there is my data
  • r

    red-dinner-66239

    10/30/2022, 12:05 PM
    it is not possible to refresh from the views ?
  • r

    red-dinner-66239

    10/30/2022, 12:06 PM
    for example make a request from the views and if it receives the request it refreshes the page?
  • b

    boundless-vase-80440

    10/30/2022, 12:11 PM
    You want to target the body in your hx-post and the data section in your hx-get?
  • r

    red-dinner-66239

    10/30/2022, 12:14 PM
    I'm just looking for a simple way to refresh my page when I get new data 😅
  • r

    red-dinner-66239

    10/30/2022, 12:16 PM
    htmx works fine, but it just closes my modal 5s later
  • b

    boundless-vase-80440

    10/30/2022, 12:19 PM
    @red-dinner-66239 need more context to help you...
  • b

    boundless-vase-80440

    10/30/2022, 12:19 PM
    DM me if you want
1...808182...100Latest