1

I have developed a data science web app that generates various statistical analysis related graphs. Statistical functions being executed from the Django app called "ProtocolApp", where I have a directory as "Statistical_protocols" while the "Stat_Learning" project is the base directory. My programme is generating some image files and .csv output file withing the base directory of project "Stat_Learning", Same directory where "manage.py" is present".

Within a template i have provided link for all the file like this:

Template:

{% extends 'protocol/base.html' %}

{% load static %}


{% block content %}

<style type="text/css">

    table {

     margin-bottom: 20px;

     border-collapse: collapse;
     border-spacing: 0;
     width: 30%;
     border: 1px solid #ddd;
     bgcolor: #00FF00;
}

th, td {
    border: none;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even){background-color: #f2f2f2}

</style>



<div style="overflow-x:auto;">
  <table align="center">
    <tr>
      <th align="center">Result files</th>
    </tr>
    {% for a in names %}
    <tr>
     {% if a %}
      <td><a href="/virtual_env_dir/Base_rectory_of_the_project/{{a}}"> {{a}} </a> <br></td>
     {% endif %}
    </tr>
    {% endfor %}
  </table>
</div>


{% endblock %}

Is there a method to provide downloadable link through this base directory for all the files.

or is there any method to add another directory called "Downloads" others then media directory. Because I am using media directory to upload the input file for the protocols.

thanks

jax
  • 3,927
  • 7
  • 41
  • 70

2 Answers2

1

Try this:

Create a view like this:

def send_file(request):
    import os, tempfile, zipfile, mimetypes
    from django.core.servers.basehttp import FileWrapper
    from django.conf import settings
    filename     = settings.BASE_DIR + <file_name>
    download_name ="example.csv"
    wrapper      = FileWrapper(open(filename))
    content_type = mimetypes.guess_type(filename)[0]
    response     = HttpResponse(wrapper,content_type=content_type)
    response['Content-Length']      = os.path.getsize(filename)    
    response['Content-Disposition'] = "attachment; filename=%s"%download_name
    return response

Create a url for that and let the anchor tag point to that url. Remember to add download attribute to your anchor tag

badiya
  • 2,247
  • 13
  • 23
yusuf.oguntola
  • 492
  • 3
  • 10
  • Can you explain little bit, what is going on here as I am quite new so its difficult for me to fit this code in my case – jax Jul 27 '17 at 18:10
  • That's how to request file download in django. You may want to read https://docs.djangoproject.com/en/1.10/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment – yusuf.oguntola Jul 28 '17 at 06:26
  • but I don't have similar type of file to server so can I define different files, as content type. – jax Jul 30 '17 at 17:44
  • Content Type is the part of the response that tells the browser what type of file to expect to successfully process it. You can simply use `application/vnd.ms-excel` in place of the `mimetypes.guess_type`. We only usually use that to be able to handle any type of file (Using the basic assumption that mimetype would guess correctly) – yusuf.oguntola Jul 31 '17 at 08:25
  • Hey thanks for your reply I have successfully build the view that can download the files from specified directory. I was struggling a lot, hence I asked similar Question but finally rectified the problem. Please review the Answer wither this method is correct and secure or not. https://stackoverflow.com/questions/45400084/how-to-serve-result-file-using-django?noredirect=1#comment77761140_45400084 – jax Jul 31 '17 at 09:02
  • Looks good. As far as that works. U may just want to use `settings.BASE_DIR` as the root folder instead of resolving the path name yourself – yusuf.oguntola Jul 31 '17 at 10:48
0

I'm not sure this quite answers your question, but the company I work for runs a Django website (1.10.5) and we tend to upload files into the media directory using the django admin panel. The admin panel also provides a page editor where you can set the URL of a page, and then drop in links to the media files. Django defines a setting that allows you to access the media library via any root url:

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = "/media/"

But if the process you define generates randomly named files, you might define a url the standard way to point to some view. The view's pseudocode might look like this:

def protocolView(request):
   someListOfDirs = ...
   context = { names: [] }
   for directory in someListOfDirs:
        for root, dirs, files in os.walk(directory):
            for file in files:
                if file is a generated file:
                     context["names"].append(file)
    render(request, "template.html", context)
Kevin Hirst
  • 876
  • 8
  • 17