pythondjangodjango-imagekit

Django imagekit 'SafeString' object has no attribute 'name'


I'm currently trying to process an image using django-imagekit in my project. I'm quite new to the package, so I'm trying to implement a simple processor that converts the image to grayscale. I followed some samples from the docs.

I decided to use spec, which I've included in models.py. However, when I wanted to generate the image on a template, I came across the following error:

AttributeError: 'SafeString' object has no attribute 'name'

FULL ERROR

Traceback (most recent call last):
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/imagekit/cachefiles/__init__.py", line 37, in __init__
    name = generator.cachefile_name
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/imagekit/specs/__init__.py", line 95, in cachefile_name
    return fn(self)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/imagekit/cachefiles/namers.py", line 40, in source_name_as_path
    '%s%s' % (generator.get_hash(), ext)))
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/imagekit/specs/__init__.py", line 134, in get_hash
    self.source.name,
AttributeError: 'SafeString' object has no attribute 'name'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/Image-Analysis-Web-App/pipeline/views.py", line 33, in playground_view
    return render(request, "playground.html", {"image": image})
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/imagekit/templatetags/imagekit.py", line 65, in render
    self._generator_kwargs)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/imagekit/templatetags/imagekit.py", line 25, in get_cachefile
    return ImageCacheFile(generator)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/imagekit/cachefiles/__init__.py", line 40, in __init__
    name = fn(generator)
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/imagekit/cachefiles/namers.py", line 91, in hash
    '%s%s' % (generator.get_hash(), ext)))
  File "/mnt/c/Users/Gilang R Ilhami/Desktop/personal_projects/random_mini_projects/image_analysis_app/app/lib/python3.7/site-packages/imagekit/specs/__init__.py", line 134, in get_hash
    self.source.name,
AttributeError: 'SafeString' object has no attribute 'name'

As I said, I'm quite new to django-imagekit. I've been searching on the web for this but had no luck so far. So if anybody could point out what I was missing, I would really appreciate it.

urls.py

from django.contrib import admin 
from django.urls import path 
from django.conf import settings 
from django.conf.urls.static import static 
from .views import *

urlpatterns = [ 
    path('', hello_world_view, name="hello_world"),
    ...,
    path('image-analysis/playground/<uuid:image_uuid>', playground_view, name='playground'),
    ...,
    path('success', success, name = 'success'), 
] 

if settings.DEBUG: 
        urlpatterns += static(settings.MEDIA_URL, 
                            document_root=settings.MEDIA_ROOT)

views.py

import os

from django.conf import settings
from django.http import HttpResponse 
from django.shortcuts import render, redirect
from numpy import uint8
from skimage import io

from .forms import *
from .models import Pipeline
from .image_processing import ImageProcessing
# Create your views here. 

def hello_world_view(request):
    return HttpResponse("<h1>HELLO</h1>")

def upload_image_view(request):

    if request.method == 'POST':

        form = PipelineForm(request.POST, request.FILES)

        if form.is_valid(): 
            form.save()
            image_uuid = form.instance.image_id
            return redirect("playground", image_uuid=image_uuid) 
    else: 
        form = PipelineForm() 
    return render(request, 'upload_image.html', {'form' : form})

def playground_view(request,image_uuid):
    image = Pipeline.objects.get(image_id=image_uuid)
    return render(request, "playground.html", {"image": image})

models.py

import uuid
from django.db import models

from imagekit import ImageSpec, register
from pipeline.processors import Grayscale

class MySpec(ImageSpec):
    processors = [
        Grayscale(),
    ]
    format = 'PNG'
    options = {'quality': 60}

register.generator('pipeline:grayscale', MySpec)

# Create your models here.

class Pipeline(models.Model):
    image_id = models.CharField(max_length=100, blank=True, unique=True, default=uuid.uuid4)
    image = models.ImageField(upload_to="image/")

playground.html

{% extends 'base.html' %}
{% load imagekit %}

{% block content %}

<div class="container">
    <div class="row">
        <div class="col-md-6">
            <h1 style="position: sticky;top:0;">Original Image</h1>
            <img style="position: sticky;top:0;" src="{{ image.image.url }}" width="500px"/>
        </div>
        <div class="col-md-6">
            <h1>Grayscale Image</h1>
            {% generateimage 'pipeline:grayscale' source="{{ image.image.url }}" -- width="500px"%}
        </div>
    </div>
</div>


{% endblock %}

Solution

  • You need to pass the image file object to the template tag, not it's URL. You also need to pass it without {{ }} brackets and quotation marks:

    {% generateimage 'pipeline:grayscale' source=image.image width="500px"%}
    

    Also drop the -- before the width argument.