Error 500 with imageField

Hello.

I am getting this error at server side:

KB07t3ihX7SFFk9KApJ3SmTb3N6mK+q0n1T/qk/NDZe3PwU2Q7Ep7KT+t+akt/T99Q/xe6/rPzX9Z+aH1f3zQmg6/vobPXD8VgpWY/AUIkbuL7JRvWH5NZY7D9BW1vl+1Qo4KX6RWaPmB+X1S8bulfg9UZfNn1wUl1gncKTFrY51qXPO1A2CwUBctgQF2ldQB5/VSvKEu0ofmma2+z8hwX6UTBKLqhFdKmkzT6pt7qaDmVJNCDZT2po0taLQ82K2HKMXgEp7fBgaupeWHt8E8KAscNAgYwVOWShcFIPmieJjkdyhhCcHlD2NN7OSew/6uOOOOCjdgQatxn6Nrp48ESSPwVZJWUEWioIKAQyiTzPmv8Az1AYDtq6TNwnzWaP+MzQWfAfoilMg9q9jGfVYtv4ZrJH+LShZ6hfho1V0h+aNVuiPxRqGiO6qO+qO+p0D3odEuqfxTorrL80jHQL8tYI/wAWlY9fwxXsY37oDAFAZvgntWvRDfiKukvcJec0s3TtqSm7BlbeCDtR2DJVhdWV8rShRwogkqP/AGAiLzrRnGwRjVEa0BK3TJrJ1f8Ak4464vQQA9DQaG49xZ6pOmZYvfw8XSSJepaDW7/+9/Z'
[17/May/2022 23:18:57] "GET /v1/dowloadAll HTTP/1.1" 500 496591

The server returns the 500 error, when it must sends an image. I think the problem is in the encoding of the image

This is the model:

class Fotos(models.Model):    
    numero = models.IntegerField(default=0)
    foto=models.ImageField(upload_to='media/', blank=True)	
    descripcion= models.TextField(blank = True)
    categoria = models.CharField(max_length=200, blank=True)

This is the view.py to upload the image:

@csrf_exempt 
def Upload (request): 
    if request.method == 'POST': 
            foto=request.POST.get("imagen0")
            descripcion=request.POST.get("descripcion")
            new=Fotos()
            new.numero=len(Fotos.objects.all())+1
            new.categoria="genérica"
            new.descripcion=descripcion
            new.foto=foto
            new.save()

What the client sends is a plaint text for the image, with this format:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/4QBsRXhpZgAASUkqAAgAAAACAA4BAgA2AAAAJgAAAJiCAgAIAAAAXAAAAAAAAABtYWQgc2NpZW50aXN0IGRvaW5nIHNvbWV0aGluZyBkYW5nZXJvdXMgYW5kIG9yIGF3ZXNvbWVCaWdfUnlhbv/hBV1odHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+Cjx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iPgoJPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KCQk8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczpwaG90b3Nob3A9Imh0dHA6Ly9ucy5hZG9iZS5jb20vcGhvdG9zaG9wLzEuMC8iIHhtbG5zOklwdGM0eG1wQ29yZT0iaHR0cDovL2lwdGMub3JnL3N0ZC9JcHRjNHhtcENvcmUvMS4wL3htbG5zLyIgICB4bWxuczpHZXR0eUltYWdlc0dJRlQ9Imh0dHA6Ly94bXAuZ2V.......

And the Download function for the image:

def DownloadAll(request):
    if request.method == 'GET':
        album= Fotos.objects.filter(numero__lte=100) #que busque todas las fotos hasta la 100        
        if album:
            array=[]
            for data in album:
                if data.foto:
                    foto={"numero":data.numero,"foto":base64.b64encode(data.foto.read()), "descripcion:":data.descripcion} 
                    array.append(foto)
            if len(array)>0: 
                return JsonResponse(array, safe=False)
            else: 
                return HttpResponse("without fotos")
        else: 
            return HttpResponse ("bad")
    else: 
        return HttpResponse("bad")

There should be a more detailed message with a traceback. It would be helpful if you posted that complete traceback.

1 Like

The problem is that I can’t. Because it returns the following:

LvkzHYqQgKB07t3ihX7SFFk9KApJ3SmTb3N6mK+q0n1T/qk/NDZe3PwU2Q7Ep7KT+t+akt/T99Q/xe6/rPzX9Z+aH1f3zQmg6/vobPXD8VgpWY/AUIkbuL7JRvWH5NZY7D9BW1vl+1Qo4KX6RWaPmB+X1S8bulfg9UZfNn1wUl1gncKTFrY51qXPO1A2CwUBctgQF2ldQB5/VSvKEu0ofmma2+z8hwX6UTBKLqhFdKmkzT6pt7qaDmVJNCDZT2po0taLQ82K2HKMXgEp7fBgaupeWHt8E8KAscNAgYwVOWShcFIPmieJjkdyhhCcHlD2NN7OSew/6uOOOOCjdgQatxn6Nrp48ESSPwVZJWUEWioIKAQyiTzPmv8Az1AYDtq6TNwnzWaP+MzQWfAfoilMg9q9jGfVYtv4ZrJH+LShZ6hfho1V0h+aNVuiPxRqGiO6qO+qO+p0D3odEuqfxTorrL80jHQL8tYI/wAWlY9fwxXsY37oDAFAZvgntWvRDfiKukvcJec0s3TtqSm7BlbeCDtR2DJVhdWV8rShRwogkqP/AGAiLzrRnGwRjVEa0BK3TJrJ1f8Ak4464vQQA9DQaG49xZ6pOmZYvfw8XSSJepaDW7/+9/Z'
[18/May/2022 11:33:03] "GET /v1/dowloadAll HTTP/1.1" 500 496593

The string continues up and I can’t see an indication that the error is on x line of views.py
And when I say that it goes up, it means that the console no longer shows that information.

How are you running your application? Are you using runserver? If so, you can redirect the output from runserver to a file.

1 Like
Watching for file changes with StatReloader
Internal Server Error: /v1/dowloadAll
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/julian/Documentos/Programacion/Album-backend/albumFotos/album/views.py", line 57, in DownloadAll
    foto={"numero":data.numero,"foto":base64.b64encode(data.foto.read()), "descripcion:":data.descripcion}
  File "/usr/lib/python3.6/site-packages/django/core/files/utils.py", line 16, in <lambda>
    read = property(lambda self: self.file.read)
  File "/usr/lib/python3.6/site-packages/django/db/models/fields/files.py", line 43, in _get_file
    self._file = self.storage.open(self.name, 'rb')
  File "/usr/lib/python3.6/site-packages/django/core/files/storage.py", line 36, in open
    return self._open(name, mode)
  File "/usr/lib/python3.6/site-packages/django/core/files/storage.py", line 224, in _open
    return File(open(self.path(name), mode))
OSError: [Errno 36] File name too long: '/home/julian/Documentos/Programacion/Album-backend/albumFotos/media/data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/4QBsRXhpZgAASUkqAAgAAAACAA4BAgA2AAAAJgAAAJiCAgAIAAAAXAAAAAAAAABtYWQgc2NpZW50aXN0IGRvaW5nIHNvbWV0aGluZyBkYW5nZXJvdXMgYW5kIG9yIGF3ZXNvbWVCaWdfUnlhbv/hBV1odHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvADw/eHBhY2tldCBiZWdpbj0i77u