jax
jax

Reputation: 4197

How to add a directory in Django for Download link ?

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

Upvotes: 1

Views: 3132

Answers (2)

yusuf.oguntola
yusuf.oguntola

Reputation: 492

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

Upvotes: 1

Kevin Hirst
Kevin Hirst

Reputation: 874

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)

Upvotes: 0

Related Questions