0
我已經開發了一個應用程序,從上傳方法需要輸入文件,而無需使用模型和運行一些代碼服務器如何使用Django服務Result文件?
這樣的背後:
/MyDjango_project_directory/media/input.csv
而且在這樣的位置產生一些結果文件。
/Virtualenv_directory/MyDjango_project_directory/OutPut_files_directory/result.csv
/Virtualenv_directory/MyDjango_project_directory/OutPut_files_directory/result.png
目前我只是移動輸出文件到「媒體」只是渲染頁面之前,通過下載鏈接一個通過「views.py」「SYS」命令文件夾都可以成功下載文件。這是爲我工作的臨時解決方案,但不是我尋找的最佳解決方案。是否有任何機構有一些連擊想法,以便我可以添加我的「output_directory」專門用於下載目的。
更新
我的觀點:
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')
網址:
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'),
]
模板:
{% 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 %}
能否請您在這裏分享您的看法碼? – viveksyngh
@viveksyngh更新謝謝 – jax