Python Django-如何从表单的输入文件标签获取文件路径? [英] Python Django- How do I get file path from an input file tag in a form?

查看:71
本文介绍了Python Django-如何从表单的输入文件标签获取文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要文件路径.到目前为止,这是我自带的:

I just need the file path. This is what I came with so far:

index.html:

index.html:

<form enctype="multipart/form-data" action="{% url 'polls:search_for_match' %}" method="post">
            {% csrf_token %}        
                <label for="file_path"></label> <input type="file"
                class="form-control" id="file_path" name="file_path" >
                <button type="submit" class="btn btn-default" >Analyze!</button>
</form>

view.py

def searchMatch(request):
    form_class = Query
    if request.method == 'POST':
        form = form_class(request.POST, request.FILES)
        **file = request.FILES['file_path']**
        last_restarts = request.POST.get('restarts' , '')
                with zipfile.ZipFile(file) as z:
                     .....

forms.py

class Query(forms.Form):
    file_path = forms.FileField()

问题出在这一行: file = request.FILES ['file_path'].read()

我没有文件路径,只有文件名.

I don't get the file path, only the file name.

推荐答案

来自客户端的文件路径可能对您没有任何用处.出于明显的安全原因,您的服务器应用程序无法访问客户端上的任意路径.

The file path from the client can't possibly be of any use to you. Your server application has no access to arbitrary paths on the client, for obvious security reasons.

文件输入提供文件本身进行上传;这就是您可以访问的所有内容,并且您需要所有.

File inputs provide the file itself for upload; that's all you can access, and all you should need.

这篇关于Python Django-如何从表单的输入文件标签获取文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆