简体   繁体   中英

Python Django- How do I get file path from an input file tag in a form?

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

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()

the problem is in this line: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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