https://htmx.org logo
You'll have to return the response object yourself...
# 🔥-django-htmx
b
You'll have to return the response object yourself with the header set, check the link David's just posted
b
Hi sorry to make a thread from this, I thought I wanted to post a solution to future reference but I wanted your opinion first on if this is smart (I have my doubts). So what I did is create a second view which actually starts the download and redirect to that one:
Copy code
python
def downloadCheck(request, pk):
    icon = f"""<img src="{static('images/arrow.svg')}">"""
    tmp = f"""download/{pk}"""
    return HttpResponse(content = format_html(f"""{icon}"""), headers = {"hx-redirect": f"""download/{pk}"""})

def download(request, pk):
    PATH = ...
    return FileResponse(open(PATH, "rb"), content_type = "image/PNG", as_attachment = True)
therefore I got another entry in the urls.py:
Copy code
python
urlpattterns = [
path("downloadcheck/<int:pk>", views.downloadCheck, name = "downloadCheck"),
path("download/<int:pk>", views.download, name = "download"),
]
can the approach with the second view be simplified? (I know I could write a CBV and check for
if request.hmx:
,?)
b
Looks like a good approach IMO
b
thanks a lot!