简体   繁体   English

Django下载文件按钮接受路径

[英]Django download file button takes in path

I am creating an app that allows users to convert their .ifc (3D model) files into other data forms (.xlsx, filtered .xlsx, json).我正在创建一个应用程序,允许用户将他们的 .ifc(3D 模型)文件转换为其他数据形式(.xlsx、过滤的 .xlsx、json)。 I have managed to implement the upload part, but now I am not sure about the download.我已经设法实现了上传部分,但现在我不确定下载。

I would like to create a "download" button that takes in the users desired download path (maybe even file name).我想创建一个“下载”按钮,接收用户所需的下载路径(甚至文件名)。 When I have the path I can start my conversion function with the last uploaded file.当我有路径时,我可以使用最后上传的文件启动我的转换功能。

def model_form_download(request):
    if request.method == 'POST':
        
        download_path = ??? #How to take in the user defined upload path?
        
        last_model = Document.objects.latest("uploaded_at") 
        last_model_name = last_model.document.name
        
        MODEL_DIR = Path(MEDIA_DIR) / last_model_name       
        model = parser(MODEL_DIR)                           

        xlsx_name = Path(last_model_name).stem                                
        
        XLS_DIR = Path(download_path) / (xlsx_name + '.xlsx')  
       
        model[1].to_excel(XLS_DIR)                          
        
        return render(request, 'core/model_form_download.html')
    return render(request, 'core/model_form_download.html')

The extra question here is how to take in the user choice of prefered conversion format and use in this view function?这里的额外问题是如何接受用户选择的首选转换格式并在此视图函数中使用?

def model_form_download(request):
    if request.method == 'POST':
        
        download_path = ??? #How to take in the user defined upload path?
        
        last_model = Document.objects.latest("uploaded_at") 
        last_model_name = last_model.document.name
        
        MODEL_DIR = Path(MEDIA_DIR) / last_model_name       
        model = parser(MODEL_DIR)                           

        xlsx_name = Path(last_model_name).stem                                
        
        XLS_DIR = Path(download_path) / (xlsx_name + '.xlsx')  
       
        model[1].to_excel(XLS_DIR)                          
        
        return render(request, 'core/model_form_download.html')
    return render(request, 'core/model_form_download.html')

How to take in the user defined upload path?如何取入用户定义的上传路径?

if you have a form on the frontend like this:如果您在前端有这样的表单:

<form>
<input name="download_path" .../>
...

Then in you django view you can access it from request然后在您的 Django 视图中,您可以从请求中访问它

download_path = request.POST.get("download_path")

the same goes for the conversion format, just add it to a form and access it from the request object.转换格式也是如此,只需将其添加到表单中并从请求对象中访问即可。

although really you should consider using forms: https://docs.djangoproject.com/en/3.2/topics/forms/虽然你真的应该考虑使用表单: https : //docs.djangoproject.com/en/3.2/topics/forms/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM