Reputation: 1
I am having trouble getting access to a simple uploaded file which I need to parse without saving it. Since the file does not need to be saved I have not made a model for it. All other threads on this state the html form encoding type, name tag are the primary reasons why request.FILES is not appearing-- I have addressed these and still there is no request.FILES being captured.
forms.py
class DataExportForm(forms.Form):
docfile = forms.FileField(label='Select a template',help_text='Extracted info will be returned')
HTML
<html>
<body>
<form action="." method="post" enctype="multipart/form-data">{% csrf_token %}
<tr><th><label for="id_docfile">Select a template:</label></th><td><input id="id
_docfile" name="docfile" type="file" />
<button type="submit" name="action">Upload</button>
<br /><span class="helptext">Zip file wil
l be returned with data</span></td></tr>
</form>
</body>
</html>
views.py
if request.method=='POST':
form=DataExportForm(request.POST, request.FILES)
if form.is_valid():
#Code runs OK till here but request.FILES does not exist.
groupList=extractTemplateNames(request.FILES['file'].read())
I guess if I get it working I may find the file not in request.FILES['file'] but in request.FILES['docfile'] but at this point request.FILES does not exist. Any tips to solve this would be appreciated.
Upvotes: 0
Views: 1784
Reputation: 1
So apparently the only thing missing was looking for request.FILES['docfile']
not request.FILES['file'] . It works! Hope this is helpful to someone
Upvotes: 0
Reputation: 12815
Most likely, problem is just that you are trying to access file using wrong name. Field on form has name docfile
. Same name it will have in request.FILES
array.
Possibly, you simply misunderstood an error message saying that there is no file
in FILES
.
And form.is_valid
access FILES array correctly, that is why form is considered to be valid.
Upvotes: 2