Reputation: 139
i have this code, the code is running well but the problem is that it did not get the file name when uploading the file. my code is -
views.py
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
if 'filename' in request.FILES:
filename = request.FILES['filename']
else:
raise Exception('did not get any name')
return HttpResponseRedirect('/user_profileform/')
else:
form = UploadFileForm()
return render_to_response('user_profile.html', {'form': form })
def handle_uploaded_file(f):
destination = open('media/filename', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
form is :-
<form action="/user_profileform/" method="POST" enctype="multipart/form-data" name="uform" id="userform">{% csrf_token %}
{{form}}
<input type="submit" value="submit" name="usubmit">
</form>
the error is :-
did not get any name
Upvotes: 0
Views: 122
Reputation: 18
You can get filename by this
filename = form.clean_data['file'].name
clean_data can access after form.is_valid() equals True
Upvotes: 1
Reputation: 8836
You can get the name of the uploaded file just by using
filename = request.FILES['filename']
.name
is not required. Refer docs for more information.
Upvotes: 1