Question

FileField does not work properly in template but in admin it works , Django , Digitalocean , Aws S3

Posted May 20, 2020 399 views
DigitalOceanDjango

I have some problems with the image field, I store them on AWS through DigitalOcean.

Fun Fact: When I upload an image on to the admin panel, it works fine, and when I’m trying to do the same in a template , image updates but does not get stored on AWS. When I try to click this image in the admin panel instead of an image I get this.

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<Error>
<Code>NoSuchKey</Code>
<BucketName>financal-space</BucketName>
<RequestId>tx000000000000004683770-005ec5341d-ab90b1-ams3b</RequestId>
<HostId>ab90b1-ams3b-ams3-zg02</HostId>
</Error>

Here is my image post request

if 'imagechange' in request.POST and request.FILES['image']:
     user = request.user.id
     image = request.FILES['image']
     fs = FileSystemStorage()
     image_fs = fs.save(image.name, image)
     image_new = CustomUser.objects.filter(user=user).update(image=image_fs)

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

So I find the solution :
I changed a little bit my POST request to this

       if 'imagechange' in request.POST and request.FILES['image']:
              user = request.user
              profile = UserMembership.objects.get(user=user)
              form = UpdateUserImage(request.POST, request.FILES or None, instance=profile)
              if form.is_valid():

                    edit = form.save(commit=False)
                    edit.save()

                    return redirect('settings')
              else:
                  return redirect('dashboard')
Submit an Answer