Raise MultiValueDictKeyError by request.FILES to get base64 image data
Actually i need to get base64 image data generated by croppie javascript library. The image data look like below.
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAgAElEQVR4Xs1dB5xU1dX/39em7c422GXpVZGmWACxImAviYnYY/1UjLF8iYkxxq4xlk+jscZek1iiiUajRiMWehEFAemwLGyfnfravd/v3Dd39zEsxpYyuO7szKv3f8r/nHvueQz/5a9NmzbFMu32KF/ju4JrIwX8XbkQ9WCoFEKU+55fLsBrOBcQnLX6QksLwdOciw7XE43cxwqmsRWciRUJs2bF5MkD8v/Nt8z+2y5uwYIt8Wg0ewDApmoMU4XAHgA0IQQAAfrlc3qvXgK+z8E5hy8EPMEgePCZ4LQHAxcCgtFvxoXgSwDxtg/xNi/zPzxm771z/01j8B8HRAhhLFmyZoLvO1M5F9MMQ5ukaZplmTo0Lbg8AoN+aNADQDT5N2MMmqbJzz3Pg0eAMID7vAgSAcfg+6ILGIlS8eX7vsO4PzuZMN+2DOPvEyaMnccY8/+TAP3HAPnkk8+HeZ44njEczzmfKEeu+KJBNgxA14IBJ2lnNNCcNIGkXpeA6HrwO/hcwBUeCp4jD6UxHa7nw3NpfOk4OmzHCY4JwLJ0xGMxdLa3orY6gdbWZlFRmZyTy2RfjOjm83vvv//G/wQw/3ZAPv109XDOxZVC4DTGmElSHpijbk0wDB2mGQDi+R503ZDmSpolj0MzTKkR9LfEkUChYzAyWb7UEAIgncmisqIanZ2dsG1XnqNffR0K+Rxy2RS456J/fR+0tjbBcWxYuiHNHQBX+PxpnbFf73PIISv/ncD82wD5/POG8a7rXsUY+w75BJJ8Jd0k/TSenPvyt2WZEMKBYWiIx2NdGkDgWZaFznSn1A4CynVd5HI5pDpTyOUyyKQ7kMsV0NHRAcfxkc3kYJgG6uv7Y9y4sXQSNGxaj2GD+iPb0Y5sLovy6t5IpTKoqqyAU7AJe4ADOrkd7rwshPjVxEMPXfDvAOZfDsiyZev6AM5vfC5m+L4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SmpVIBf2M+o76MEciu6bMGGgM9OGRI2FrEjh844l+MPbz8OJZVHTK4nekQE4ZNRx6GsNQ9SLdGVze/JbdP2lpqpLCIojW6oRasBVlpgAiUaj8Fy+sibdPm70jBlUjbHDa6eAfPbJJ9dms5lrKBLnxcIBJphkSWqwv0iaw0xI2X3l0NVFKtNGx1HapgY3DI4yJbQfbae0UdFedTzNpOSkD803EdEttGQaYdZwvL/0Dcxa/xx4TQE5cNhpDeNqpuGk8ReitzcYHlq6TFZYYNQ1hK8vnC0u1ZiwGVMjrdI6ASAxWaTh+P7Ppx9zxC1fGpAlc5fs4rr5pbqpRbjMmPoBY/E4GA+qRIiYBFIRnmFTMyH0WVCaE3bWYbMUdphKc3YGcKlzDWuoMhlSgjUypwKaTzOLOrJ+BxpSG/C3j15DZCDDitQC2FYKum9gj+T+mLHHuahy+sBmlFHojlGUxocBCfsudR89aUXpZ0pYaHaTfK7vUe7NTdsuG3Xk947cYcaxRw35ePbsxznnZwTRdHeJjpztL5nkCT
Here is my HTML
<form action="{% url 'profile' %}" enctype="multipart/form-data" method="post" class="pic-upload-form" id="form">{% csrf_token %}
<div class="custom-file">
{% bootstrap_form form %}
</div>
<!-- Croppie area -->
<div id="upload-demo"></div>
<input type="hidden" id="image-data" name="imagebase64">
<button type="button" class="btn btn-outline-info btn-custom upload-result">save</button>
</form>
The last portion of my js where i bind the image data to a hidden input.
$('.upload-result').on('click', function (ev) {
$uploadCrop.croppie('result', {
type: 'base64',
size: 'viewport'
}).then(function (src) {
console.log(src);
// bind the image data to the hidden inpu id image-data
$('#image-data').attr('src', src);
$('#form').submit();
});
});
And here is my view
def user_profile_view(request):
'''Handle the image of the user.'''
if request.method == 'POST':
form = UploadPicForm(request.POST, request.FILES)
if form.is_valid():
profile = form.save(commit=False)
# get the base64 image data
profile.image = request.FILES['imagebase64']
profile.save()
messages.success(request, 'your pic updated successfully.')
return HttpResponseRedirect(reverse('learning_path_tracker:home'))
else:
# form's user field populated by current user
form = UploadPicForm(initial={'user': request.user})
return render(request, 'users/uploadpic.html', {'form': form})
I think the image data is not a string that's why request.FILES raise error. But how can i solve this, thanks in advance.
django croppie
add a comment |
Actually i need to get base64 image data generated by croppie javascript library. The image data look like below.
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAgAElEQVR4Xs1dB5xU1dX/39em7c422GXpVZGmWACxImAviYnYY/1UjLF8iYkxxq4xlk+jscZek1iiiUajRiMWehEFAemwLGyfnfravd/v3Dd39zEsxpYyuO7szKv3f8r/nHvueQz/5a9NmzbFMu32KF/ju4JrIwX8XbkQ9WCoFEKU+55fLsBrOBcQnLX6QksLwdOciw7XE43cxwqmsRWciRUJs2bF5MkD8v/Nt8z+2y5uwYIt8Wg0ewDApmoMU4XAHgA0IQQAAfrlc3qvXgK+z8E5hy8EPMEgePCZ4LQHAxcCgtFvxoXgSwDxtg/xNi/zPzxm771z/01j8B8HRAhhLFmyZoLvO1M5F9MMQ5ukaZplmTo0Lbg8AoN+aNADQDT5N2MMmqbJzz3Pg0eAMID7vAgSAcfg+6ILGIlS8eX7vsO4PzuZMN+2DOPvEyaMnccY8/+TAP3HAPnkk8+HeZ44njEczzmfKEeu+KJBNgxA14IBJ2lnNNCcNIGkXpeA6HrwO/hcwBUeCp4jD6UxHa7nw3NpfOk4OmzHCY4JwLJ0xGMxdLa3orY6gdbWZlFRmZyTy2RfjOjm83vvv//G/wQw/3ZAPv109XDOxZVC4DTGmElSHpijbk0wDB2mGQDi+R503ZDmSpolj0MzTKkR9LfEkUChYzAyWb7UEAIgncmisqIanZ2dsG1XnqNffR0K+Rxy2RS456J/fR+0tjbBcWxYuiHNHQBX+PxpnbFf73PIISv/ncD82wD5/POG8a7rXsUY+w75BJJ8Jd0k/TSenPvyt2WZEMKBYWiIx2NdGkDgWZaFznSn1A4CynVd5HI5pDpTyOUyyKQ7kMsV0NHRAcfxkc3kYJgG6uv7Y9y4sXQSNGxaj2GD+iPb0Y5sLovy6t5IpT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pm/tJBzdiVzQFG4Jv1BkIsK23x1IWFJCQ+sujC6COV4w9KnpE0626IPKqWWSmpVIBf2M+o76MEciu6bMGGgM9OGRI2FrEjh844l+MPbz8OJZVHTK4nekQE4ZNRx6GsNQ9SLdGVze/JbdP2lpqpLCIojW6oRasBVlpgAiUaj8Fy+sibdPm70jBlUjbHDa6eAfPbJJ9dms5lrKBLnxcIBJphkSWqwv0iaw0xI2X3l0NVFKtNGx1HapgY3DI4yJbQfbae0UdFedTzNpOSkD803EdEttGQaYdZwvL/0Dcxa/xx4TQE5cNhpDeNqpuGk8ReitzcYHlq6TFZYYNQ1hK8vnC0u1ZiwGVMjrdI6ASAxWaTh+P7Ppx9zxC1fGpAlc5fs4rr5pbqpRbjMmPoBY/E4GA+qRIiYBFIRnmFTMyH0WVCaE3bWYbMUdphKc3YGcKlzDWuoMhlSgjUypwKaTzOLOrJ+BxpSG/C3j15DZCDDitQC2FYKum9gj+T+mLHHuahy+sBmlFHojlGUxocBCfsudR89aUXpZ0pYaHaTfK7vUe7NTdsuG3Xk947cYcaxRw35ePbsxznnZwTRdHeJjpztL5nkCT
Here is my HTML
<form action="{% url 'profile' %}" enctype="multipart/form-data" method="post" class="pic-upload-form" id="form">{% csrf_token %}
<div class="custom-file">
{% bootstrap_form form %}
</div>
<!-- Croppie area -->
<div id="upload-demo"></div>
<input type="hidden" id="image-data" name="imagebase64">
<button type="button" class="btn btn-outline-info btn-custom upload-result">save</button>
</form>
The last portion of my js where i bind the image data to a hidden input.
$('.upload-result').on('click', function (ev) {
$uploadCrop.croppie('result', {
type: 'base64',
size: 'viewport'
}).then(function (src) {
console.log(src);
// bind the image data to the hidden inpu id image-data
$('#image-data').attr('src', src);
$('#form').submit();
});
});
And here is my view
def user_profile_view(request):
'''Handle the image of the user.'''
if request.method == 'POST':
form = UploadPicForm(request.POST, request.FILES)
if form.is_valid():
profile = form.save(commit=False)
# get the base64 image data
profile.image = request.FILES['imagebase64']
profile.save()
messages.success(request, 'your pic updated successfully.')
return HttpResponseRedirect(reverse('learning_path_tracker:home'))
else:
# form's user field populated by current user
form = UploadPicForm(initial={'user': request.user})
return render(request, 'users/uploadpic.html', {'form': form})
I think the image data is not a string that's why request.FILES raise error. But how can i solve this, thanks in advance.
django croppie
add a comment |
Actually i need to get base64 image data generated by croppie javascript library. The image data look like below.
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAgAElEQVR4Xs1dB5xU1dX/39em7c422GXpVZGmWACxImAviYnYY/1UjLF8iYkxxq4xlk+jscZek1iiiUajRiMWehEFAemwLGyfnfravd/v3Dd39zEsxpYyuO7szKv3f8r/nHvueQz/5a9NmzbFMu32KF/ju4JrIwX8XbkQ9WCoFEKU+55fLsBrOBcQnLX6QksLwdOciw7XE43cxwqmsRWciRUJs2bF5MkD8v/Nt8z+2y5uwYIt8Wg0ewDApmoMU4XAHgA0IQQAAfrlc3qvXgK+z8E5hy8EPMEgePCZ4LQHAxcCgtFvxoXgSwDxtg/xNi/zPzxm771z/01j8B8HRAhhLFmyZoLvO1M5F9MMQ5ukaZplmTo0Lbg8AoN+aNADQDT5N2MMmqbJzz3Pg0eAMID7vAgSAcfg+6ILGIlS8eX7vsO4PzuZMN+2DOPvEyaMnccY8/+TAP3HAPnkk8+HeZ44njEczzmfKEeu+KJBNgxA14IBJ2lnNNCcNIGkXpeA6HrwO/hcwBUeCp4jD6UxHa7nw3NpfOk4OmzHCY4JwLJ0xGMxdLa3orY6gdbWZlFRmZyTy2RfjOjm83vvv//G/wQw/3ZAPv109XDOxZVC4DTGmElSHpijbk0wDB2mGQDi+R503ZDmSpolj0MzTKkR9LfEkUChYzAyWb7UEAIgncmisqIanZ2dsG1XnqNffR0K+Rxy2RS456J/fR+0tjbBcWxYuiHNHQBX+PxpnbFf73PIISv/ncD82wD5/POG8a7rXsUY+w75BJJ8Jd0k/TSenPvyt2WZEMKBYWiIx2NdGkDgWZaFznSn1A4CynVd5HI5pDpTyOUyyKQ7kMs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BulO+Qcg+fVwERNlfqs7BGld5fWHsIkEgkCuGLKQccMf0fOwNlp4AsW7bMMpm/tJBzdiVzQFG4Jv1BkIsK23x1IWFJCQ+sujC6COV4w9KnpE0626IPKqWWSmpVIBf2M+o76MEciu6bMGGgM9OGRI2FrEjh844l+MPbz8OJZVHTK4nekQE4ZNRx6GsNQ9SLdGVze/JbdP2lpqpLCIojW6oRasBVlpgAiUaj8Fy+sibdPm70jBlUjbHDa6eAfPbJJ9dms5lrKBLnxcIBJphkSWqwv0iaw0xI2X3l0NVFKtNGx1HapgY3DI4yJbQfbae0UdFedTzNpOSkD803EdEttGQaYdZwvL/0Dcxa/xx4TQE5cNhpDeNqpuGk8ReitzcYHlq6TFZYYNQ1hK8vnC0u1ZiwGVMjrdI6ASAxWaTh+P7Ppx9zxC1fGpAlc5fs4rr5pbqpRbjMmPoBY/E4GA+qRIiYBFIRnmFTMyH0WVCaE3bWYbMUdphKc3YGcKlzDWuoMhlSgjUypwKaTzOLOrJ+BxpSG/C3j15DZCDDitQC2FYKum9gj+T+mLHHuahy+sBmlFHojlGUxocBCfsudR89aUXpZ0pYaHaTfK7vUe7NTdsuG3Xk947cYcaxRw35ePbsxznnZwTRdHeJjpztL5nkCT
Here is my HTML
<form action="{% url 'profile' %}" enctype="multipart/form-data" method="post" class="pic-upload-form" id="form">{% csrf_token %}
<div class="custom-file">
{% bootstrap_form form %}
</div>
<!-- Croppie area -->
<div id="upload-demo"></div>
<input type="hidden" id="image-data" name="imagebase64">
<button type="button" class="btn btn-outline-info btn-custom upload-result">save</button>
</form>
The last portion of my js where i bind the image data to a hidden input.
$('.upload-result').on('click', function (ev) {
$uploadCrop.croppie('result', {
type: 'base64',
size: 'viewport'
}).then(function (src) {
console.log(src);
// bind the image data to the hidden inpu id image-data
$('#image-data').attr('src', src);
$('#form').submit();
});
});
And here is my view
def user_profile_view(request):
'''Handle the image of the user.'''
if request.method == 'POST':
form = UploadPicForm(request.POST, request.FILES)
if form.is_valid():
profile = form.save(commit=False)
# get the base64 image data
profile.image = request.FILES['imagebase64']
profile.save()
messages.success(request, 'your pic updated successfully.')
return HttpResponseRedirect(reverse('learning_path_tracker:home'))
else:
# form's user field populated by current user
form = UploadPicForm(initial={'user': request.user})
return render(request, 'users/uploadpic.html', {'form': form})
I think the image data is not a string that's why request.FILES raise error. But how can i solve this, thanks in advance.
django croppie
Actually i need to get base64 image data generated by croppie javascript library. The image data look like below.
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAgAElEQVR4Xs1dB5xU1dX/39em7c422GXpVZGmWACxImAviYnYY/1UjLF8iYkxxq4xlk+jscZek1iiiUajRiMWehEFAemwLGyfnfravd/v3Dd39zEsxpYyuO7szKv3f8r/nHvueQz/5a9NmzbFMu32KF/ju4JrIwX8XbkQ9WCoFEKU+55fLsBrOBcQnLX6QksLwdOciw7XE43cxwqmsRWciRUJs2bF5MkD8v/Nt8z+2y5uwYIt8Wg0ewDApmoMU4XAHgA0IQQAAfrlc3qvXgK+z8E5hy8EPMEgePCZ4LQHAxcCgtFvxoXgSwDxtg/xNi/zPzxm771z/01j8B8HRAhhLFmyZoLvO1M5F9MMQ5ukaZplmTo0Lbg8AoN+aNADQDT5N2MMmqbJzz3Pg0eAMID7vAgSAcfg+6ILGIlS8eX7vsO4PzuZMN+2DOPvEyaMnccY8/+TAP3HAPnkk8+HeZ44njEczzmfKEeu+KJBNgxA14IBJ2lnNNCcNIGkXpeA6HrwO/hcwBUeCp4jD6UxHa7nw3NpfOk4OmzHCY4JwLJ0xGMxdLa3orY6gdbWZlFRmZyTy2RfjOjm83vvv//G/wQw/3ZAPv109XDOxZVC4DTGmElSHpijbk0wDB2mGQDi+R503ZDmSpolj0MzTKkR9LfEkUChYzAyWb7UEAIgncmisqIanZ2dsG1XnqNffR0K+Rxy2RS456J/fR+0tjbBcWxYuiHNHQBX+PxpnbFf73PIISv/ncD82wD5/POG8a7rXsUY+w75BJJ8Jd0k/TSenPvyt2WZ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WmFIrGdRgwUMjnkSiPIefmoZlJOKYHlOfRmm1AR0s7mte1YK+RE1BulO+Qcg+fVwERNlfqs7BGld5fWHsIkEgkCuGLKQccMf0fOwNlp4AsW7bMMpm/tJBzdiVzQFG4Jv1BkIsK23x1IWFJCQ+sujC6COV4w9KnpE0626IPKqWWSmpVIBf2M+o76MEciu6bMGGgM9OGRI2FrEjh844l+MPbz8OJZVHTK4nekQE4ZNRx6GsNQ9SLdGVze/JbdP2lpqpLCIojW6oRasBVlpgAiUaj8Fy+sibdPm70jBlUjbHDa6eAfPbJJ9dms5lrKBLnxcIBJphkSWqwv0iaw0xI2X3l0NVFKtNGx1HapgY3DI4yJbQfbae0UdFedTzNpOSkD803EdEttGQaYdZwvL/0Dcxa/xx4TQE5cNhpDeNqpuGk8ReitzcYHlq6TFZYYNQ1hK8vnC0u1ZiwGVMjrdI6ASAxWaTh+P7Ppx9zxC1fGpAlc5fs4rr5pbqpRbjMmPoBY/E4GA+qRIiYBFIRnmFTMyH0WVCaE3bWYbMUdphKc3YGcKlzDWuoMhlSgjUypwKaTzOLOrJ+BxpSG/C3j15DZCDDitQC2FYKum9gj+T+mLHHuahy+sBmlFHojlGUxocBCfsudR89aUXpZ0pYaHaTfK7vUe7NTdsuG3Xk947cYcaxRw35ePbsxznnZwTRdHeJjpztL5nkCT
Here is my HTML
<form action="{% url 'profile' %}" enctype="multipart/form-data" method="post" class="pic-upload-form" id="form">{% csrf_token %}
<div class="custom-file">
{% bootstrap_form form %}
</div>
<!-- Croppie area -->
<div id="upload-demo"></div>
<input type="hidden" id="image-data" name="imagebase64">
<button type="button" class="btn btn-outline-info btn-custom upload-result">save</button>
</form>
The last portion of my js where i bind the image data to a hidden input.
$('.upload-result').on('click', function (ev) {
$uploadCrop.croppie('result', {
type: 'base64',
size: 'viewport'
}).then(function (src) {
console.log(src);
// bind the image data to the hidden inpu id image-data
$('#image-data').attr('src', src);
$('#form').submit();
});
});
And here is my view
def user_profile_view(request):
'''Handle the image of the user.'''
if request.method == 'POST':
form = UploadPicForm(request.POST, request.FILES)
if form.is_valid():
profile = form.save(commit=False)
# get the base64 image data
profile.image = request.FILES['imagebase64']
profile.save()
messages.success(request, 'your pic updated successfully.')
return HttpResponseRedirect(reverse('learning_path_tracker:home'))
else:
# form's user field populated by current user
form = UploadPicForm(initial={'user': request.user})
return render(request, 'users/uploadpic.html', {'form': form})
I think the image data is not a string that's why request.FILES raise error. But how can i solve this, thanks in advance.
django croppie
django croppie
asked Jan 1 at 12:07
squal squal
105110
105110
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
request.FILES
only contain the files uploaded using input type="file"
. Whereas you're sending the image data as a base64 using type="hidden"
which sends data as string, not as file.
So, the image should be present in request.POST
instead of request.FILES
.
Do this:
profile.image = request.POST['imagebase64']
As a side note, instead of accessing keys directly from a dictionary, you should use the dict's get()
method because it let's you provide a default value if the requested key is not present in the dictionary.
profile.image = request.POST.get('imagebase64', "default value")
Appreciate with you, but how do i check profile.image gets the data?@xyres
– squal
Jan 1 at 19:47
@squal Isprofile.image
anImageField
or something else?
– xyres
Jan 1 at 20:40
Yes, that isImageField
– squal
Jan 1 at 21:02
@squal In that case you'll have to upload the image as a regular file instead of base64.
– xyres
Jan 1 at 21:04
I need to save the croped image deliver by croppie javascript library. For this time, I am able to send the base64 image data to the view, but not still able to save that. What should i do now? By the way i am here learning to handle to decode the base64 image data.
– squal
Jan 1 at 21:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995301%2fraise-multivaluedictkeyerror-by-request-files-to-get-base64-image-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
request.FILES
only contain the files uploaded using input type="file"
. Whereas you're sending the image data as a base64 using type="hidden"
which sends data as string, not as file.
So, the image should be present in request.POST
instead of request.FILES
.
Do this:
profile.image = request.POST['imagebase64']
As a side note, instead of accessing keys directly from a dictionary, you should use the dict's get()
method because it let's you provide a default value if the requested key is not present in the dictionary.
profile.image = request.POST.get('imagebase64', "default value")
Appreciate with you, but how do i check profile.image gets the data?@xyres
– squal
Jan 1 at 19:47
@squal Isprofile.image
anImageField
or something else?
– xyres
Jan 1 at 20:40
Yes, that isImageField
– squal
Jan 1 at 21:02
@squal In that case you'll have to upload the image as a regular file instead of base64.
– xyres
Jan 1 at 21:04
I need to save the croped image deliver by croppie javascript library. For this time, I am able to send the base64 image data to the view, but not still able to save that. What should i do now? By the way i am here learning to handle to decode the base64 image data.
– squal
Jan 1 at 21:14
add a comment |
request.FILES
only contain the files uploaded using input type="file"
. Whereas you're sending the image data as a base64 using type="hidden"
which sends data as string, not as file.
So, the image should be present in request.POST
instead of request.FILES
.
Do this:
profile.image = request.POST['imagebase64']
As a side note, instead of accessing keys directly from a dictionary, you should use the dict's get()
method because it let's you provide a default value if the requested key is not present in the dictionary.
profile.image = request.POST.get('imagebase64', "default value")
Appreciate with you, but how do i check profile.image gets the data?@xyres
– squal
Jan 1 at 19:47
@squal Isprofile.image
anImageField
or something else?
– xyres
Jan 1 at 20:40
Yes, that isImageField
– squal
Jan 1 at 21:02
@squal In that case you'll have to upload the image as a regular file instead of base64.
– xyres
Jan 1 at 21:04
I need to save the croped image deliver by croppie javascript library. For this time, I am able to send the base64 image data to the view, but not still able to save that. What should i do now? By the way i am here learning to handle to decode the base64 image data.
– squal
Jan 1 at 21:14
add a comment |
request.FILES
only contain the files uploaded using input type="file"
. Whereas you're sending the image data as a base64 using type="hidden"
which sends data as string, not as file.
So, the image should be present in request.POST
instead of request.FILES
.
Do this:
profile.image = request.POST['imagebase64']
As a side note, instead of accessing keys directly from a dictionary, you should use the dict's get()
method because it let's you provide a default value if the requested key is not present in the dictionary.
profile.image = request.POST.get('imagebase64', "default value")
request.FILES
only contain the files uploaded using input type="file"
. Whereas you're sending the image data as a base64 using type="hidden"
which sends data as string, not as file.
So, the image should be present in request.POST
instead of request.FILES
.
Do this:
profile.image = request.POST['imagebase64']
As a side note, instead of accessing keys directly from a dictionary, you should use the dict's get()
method because it let's you provide a default value if the requested key is not present in the dictionary.
profile.image = request.POST.get('imagebase64', "default value")
answered Jan 1 at 16:40
xyresxyres
9,74732445
9,74732445
Appreciate with you, but how do i check profile.image gets the data?@xyres
– squal
Jan 1 at 19:47
@squal Isprofile.image
anImageField
or something else?
– xyres
Jan 1 at 20:40
Yes, that isImageField
– squal
Jan 1 at 21:02
@squal In that case you'll have to upload the image as a regular file instead of base64.
– xyres
Jan 1 at 21:04
I need to save the croped image deliver by croppie javascript library. For this time, I am able to send the base64 image data to the view, but not still able to save that. What should i do now? By the way i am here learning to handle to decode the base64 image data.
– squal
Jan 1 at 21:14
add a comment |
Appreciate with you, but how do i check profile.image gets the data?@xyres
– squal
Jan 1 at 19:47
@squal Isprofile.image
anImageField
or something else?
– xyres
Jan 1 at 20:40
Yes, that isImageField
– squal
Jan 1 at 21:02
@squal In that case you'll have to upload the image as a regular file instead of base64.
– xyres
Jan 1 at 21:04
I need to save the croped image deliver by croppie javascript library. For this time, I am able to send the base64 image data to the view, but not still able to save that. What should i do now? By the way i am here learning to handle to decode the base64 image data.
– squal
Jan 1 at 21:14
Appreciate with you, but how do i check profile.image gets the data?@xyres
– squal
Jan 1 at 19:47
Appreciate with you, but how do i check profile.image gets the data?@xyres
– squal
Jan 1 at 19:47
@squal Is
profile.image
an ImageField
or something else?– xyres
Jan 1 at 20:40
@squal Is
profile.image
an ImageField
or something else?– xyres
Jan 1 at 20:40
Yes, that is
ImageField
– squal
Jan 1 at 21:02
Yes, that is
ImageField
– squal
Jan 1 at 21:02
@squal In that case you'll have to upload the image as a regular file instead of base64.
– xyres
Jan 1 at 21:04
@squal In that case you'll have to upload the image as a regular file instead of base64.
– xyres
Jan 1 at 21:04
I need to save the croped image deliver by croppie javascript library. For this time, I am able to send the base64 image data to the view, but not still able to save that. What should i do now? By the way i am here learning to handle to decode the base64 image data.
– squal
Jan 1 at 21:14
I need to save the croped image deliver by croppie javascript library. For this time, I am able to send the base64 image data to the view, but not still able to save that. What should i do now? By the way i am here learning to handle to decode the base64 image data.
– squal
Jan 1 at 21:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995301%2fraise-multivaluedictkeyerror-by-request-files-to-get-base64-image-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown