I am trying to make static files download-able from the template of my Django app.
def list(request):
folder = '/home/galander/Desktop/Projekty/django-pdf-generator/django-pdf/generator/static/pdfs'
file_list = os.listdir(folder)
return render_to_response('list.html', {'file_list': file_list})
def download_file(request):
pdf_folder = '/home/galander/Desktop/Projekty/django-pdf-generator/django-pdf/generator/static/pdfs'
response = HttpResponse(pdf_folder, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="nowy.pdf"'
return response
list.html
{% for file in file_list %}
<a href="/home/galander/Desktop/Projekty/django-pdf-generator/django-pdf/generator/static/pdfs/{{ file }}">{{ file }}</a>
{% endfor %}
My current output is rather obvious at the moment - Django is looking for matching url pattern and it fails. My filename="nowy.odf" in download_file(request): is hard-coded atm just for testing purposes.
2 Solutions I am thinking of:
1) Create appropriate regex for url pattern in url.py to satisfy redirection to my download_file view which i fail to accomplish 2) I should change the display method for my static/pdf folder somehow
update
I have previously add STATIC_URL and STATIC_ROOT as also run collectstatic ( my app is generating pdfs and saving them into static folder, that was required to do so). I've also added URL pattern part (with serve, not static_view.serve becuase docs say so).
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
if settings.DEBUG:
urlpatterns += [
url(
r'^media/(?P<path>.*)$', serve, # NOQA
{'document_root': settings.MEDIA_ROOT,
'show_indexes': True}
),
] + staticfiles_urlpatterns() # NOQA
Unfortunately I don't understand the path with static upfront
<a href="{% static 'django-pdf/generator/static/pdfs/nowy.pdf' %}">{{ file }}</a>
To be more accurate, how the static works here and what does that mean. Current output just shows that the file wasn't found. App doesn't work in debug mode atm.