I am new to Django and I need some assistance. I want to download the file whose path is in plik.localisation
(it has been taken from a loop). The problem is with the path. Could someone help me? What am I doing wrong?
My view.py is:
def download(request, path):
file_path = os.path.join(settings.MEDIA_ROOT, path)
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
response = HttpResponse(fh.read(), content_type="application/pdf")
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
return response
raise Http404
Urls
path('download/<str:path>',views.download, name="download"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Html
<div class="com">
{{plik.localisation}} <a href="{% url 'download/path' %}" class="button">download</a>
</div>
I've tried to change that path but still have problem with it.
path()
with thename
'download/path'
in theurlpatterns
list you shared so you will get a 404 HTTP response status when you click that anchor tag. There is"download"
though. But what will be the argument for thatView
then? 2. If you use"download/path"
as the argument for thatView
but you do not have a file"download/path"
in yoursettings.MEDIA_ROOT
, theif
block will never be entered so you will get a 404 HTTP response status also. If you understood this comment I’ll write an answer but if you didn’t, ask for more clarification.Model
for theQuerySet
plik
.