I have developed an App that takes input file from the upload method without using model and run some code behind the server
like this:
/MyDjango_project_directory/media/input.csv
And generates some result files in a location like this.
/Virtualenv_directory/MyDjango_project_directory/OutPut_files_directory/result.csv
/Virtualenv_directory/MyDjango_project_directory/OutPut_files_directory/result.png
Presently I am just moving the output files into "media" folder through "sys" command in "views.py" just before rendering the result page and through download link one can download the files successfully. This is a temporary solution which is working for me but not the best solution for which I am looking for. Does any body have some batter idea so that I can add my "output_directory" dedicate for the download purposes.
updated
My views:
from django.shortcuts import render
from django.core.files.storage import FileSystemStorage
from django.shortcuts import render, redirect
from django.conf import settings
from django.core.files.storage import FileSystemStorage
import os
import glob
from django.core.files.storage import FileSystemStorage
def Home_page(request):
return render(request, 'protocol/home.html', {})
#def Main_protocol(request):
# return render(request, 'protocol/main_protocol.html', {}
def simple_upload(request):
result_files_list = []
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
os.system("python /home/user/Desktop/pep_learn_project/new_pep_src/protocol/PEP_learn_1.0_selected/Sample_protocol.py > pro.log")
os.system("rm /home/user/Desktop/pep_learn_project/new_pep_src/media/*.csv")
base_link = "/home/user/Desktop/pep_learn_project/new_pep_src/"
names = []
files_to_download = glob.glob("/path_to_files/*.*")
for i, f in enumerate(files_to_download):
if f.split(".")[1] in ["csv", "jpg"]:
names.append(files_to_download[i].split("/")[6])
return render(request, 'protocol/successful.html', {
'names': names, 'base_link':base_link
})
return render(request, 'protocol/main_protocol.html')
urls:
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.Home_page, name='Home_page'),
url(r'^protocol/$', views.simple_upload, name='simple_upload'),
]
template:
{% 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="/media/{{a}}"> {{a}} </a> <br></td>
{% endif %}
</tr>
{% endfor %}
</table>
</div>
{% endblock %}