I want to be able to download files on my Django website by clicking on a link. But even by following some tutorials it’s not working. So first, I have a function that is generating files when I click on a button. This function is working fine, this is not the problem. Those files are being displayed on the page, and this is also working fine. The problem is about the downloading part. The page that is displaying the file names as links
This is the function that is rendering the page and that is returning a list with all the files in a directory. And then the other part of this function is supposed to let me download the files by clicking their link.
# views.py
def print_ip(request, filename=''):
if request.method == "POST":
get_ipfile(request)
ip_files_list = os.listdir("mysite/ip_print_files/")
if filename != '':
filepath = "mysite/ip_print_files/" + filename
path = open(filepath, 'rb')
mime_type, _ = mimetypes.guess_type(filepath)
response = HttpResponse(path, content_type=mime_type)
response['Content-Disposition'] = "attachment; filename=%s" % filename
return response
else:
return render(request, "print_ip.html", {"files": ip_files_list})
And this is the HTML file of the page shown in the previous image. As you can see in the image above, the file names are being displayed correctly but I’m stuck for the part of being able to use the links.
{% extends "layout.html" %}
{% block title %}
Print Ip
{% endblock %}
{% block main %}
<h1>Print Ip</h1>
{% for file in files %}
<p><a href="{{ file }}">{{ file }}</a></p>
{% endfor %}
<form method="POST">
{% csrf_token %}
<input type="submit" value="Print Ip Now">
</form>
{% endblock %}
3
Answers
Ok thank you all. So I think like you said @Bruno it's not possible to take file from the server and download it. Or if it's possible it's too complicated. Again, thank you all for your time.
I think we should close this question.
First of all as @Marat pointed out your parameter
filename=''
is never changed in the function, therefor the blockif filename != '':
is never executed. Tryif filename == '':
instead.Not sure if below is an answer to your question, but it shows how to download a file from the browser in Django.
To select a file in a template you can do this with
<input type="file" .../>
, note theform
needs to have the statementenctype="multipart/form-data"
.The file is then posted in the view with
request.FILES['file_name']
. Once you have it you can do whatever you want, in the example below I display the file in the same tab. Note in the example this only works for pdf documents.A minimal example of file selection and display in the browser is below.
view_select_file.py
select_file_template.html
This is more of a shared scratchpad than an answer. I am going to delete it later.
From a previous discussion in comments: the files are dynamically generated (i.e., the content is different every time), but take no parameters.
Changes explained:
filename
is to be passed as a GET parameter. Also, semantics of HTTP request types is that POST is changing something on server<a href=..>
links will do.The template: