Django imagekit 'SafeString' 对象没有属性 'name'
Django imagekit 'SafeString' object has no attribute 'name'
我目前正尝试在我的项目中使用 django-imagekit 处理图像。我对这个包很陌生,所以我正在尝试实现一个将图像转换为灰度的简单处理器。我关注了 docs.
中的一些示例
我决定使用我在 models.py
中包含的规范。但是,当我想在模板上生成图像时,遇到了以下错误:
AttributeError: 'SafeString' object has no attribute 'name'
完全错误
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'
正如我所说,我对 django-imagekit 很陌生。我一直在网上搜索这个,但到目前为止没有运气。所以如果有人能指出我遗漏了什么,我将不胜感激。
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 %}
您需要将图像文件对象传递给模板标签,而不是URL。您还需要在不带 {{ }}
括号和引号的情况下传递它:
{% generateimage 'pipeline:grayscale' source=image.image width="500px"%}
同时在 width
参数之前删除 --
。
我目前正尝试在我的项目中使用 django-imagekit 处理图像。我对这个包很陌生,所以我正在尝试实现一个将图像转换为灰度的简单处理器。我关注了 docs.
中的一些示例我决定使用我在 models.py
中包含的规范。但是,当我想在模板上生成图像时,遇到了以下错误:
AttributeError: 'SafeString' object has no attribute 'name'
完全错误
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'
正如我所说,我对 django-imagekit 很陌生。我一直在网上搜索这个,但到目前为止没有运气。所以如果有人能指出我遗漏了什么,我将不胜感激。
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 %}
您需要将图像文件对象传递给模板标签,而不是URL。您还需要在不带 {{ }}
括号和引号的情况下传递它:
{% generateimage 'pipeline:grayscale' source=image.image width="500px"%}
同时在 width
参数之前删除 --
。