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 %} 
+0

能否請您在這裏分享您的看法碼? – viveksyngh

+0

@viveksyngh更新謝謝 – jax

回答

0

掙扎了很多之後最後我都寫下來,我需要的東西:

意見是:

def Download_files(request, file_name): 

     file_path = os.path.join('/out_put_files', file_name) 
     file_wrapper = FileWrapper(file(file_path,'rb')) 
     file_mimetype = mimetypes.guess_type(file_path) 
     response = HttpResponse(file_wrapper, content_type=file_mimetype) 
     response['X-Sendfile'] = file_path 
     response['Content-Length'] = os.stat(file_path).st_size 
     response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name) 

     return response 

和網址:

url(r'^protocol/(?P<file_name>.+)$', views.Download_files, name='Download_files'), 

我回答我的問題這意味着我剛剛得知這個小會兒前,並張貼在這裏,如果有人可以從中受益。如果有任何專家遇到這個問題,請仔細檢查一下,否則它會變成一個醜陋的解決方案或適當的解決方案,並且在部署過程中它還能工作嗎?謝謝。

這個問題已經幫了我很多理解和貫徹的理念:Downloading the files(which are uploaded) from media folder in django 1.4.3

相關問題