In my admin I have a section for editing products and for each product has a one to many of sample images. I wanted to display the image for convience in my StackedInline for the sample images. However after setting everything up to display the sample image, I am still getting an internal error when it tries to render the view. Can someone look at it and give me an idea of whats going on?
class SampleImage(models.Model):
sheet = models.ForeignKey(Sheet, related_name = "samples", on_delete=models.CASCADE, null=True)
sample_type = models.IntegerField(choices=IMAGE_CHOICES, default=IMAGE_CHOICES[-1][0])
sample_image = models.ImageField(upload_to=sample_image_upload_to)
order = models.IntegerField(blank=True, null=True)
def display_sample_image(self):
width = 200
height = width * self.sample_image.height / self.sample_image.width
return format_html('<img src="' + self.sample_image.url + '" width=' + str(width) + ' height=' + str(height) + ">")
display_sample_image.short_description = "Image"
class Meta:
ordering = ('order',)
I'm getting two errors, the first is:
Template error:
In template E:\Python\Python37\lib\site-packages\django\contrib\admin\templates\admin\includes\fieldset.html, error at line 17
That causes a second error:
Exception Type: FileNotFoundError at /admin/browse/sheet/3868/change/
Exception Value: [Errno 2] No such file or directory: '[....].png'
I'm guessing its still not finding the display_sample_image field? Any thoughts on what I might be missing? The raw detail is below. I appreciate any help anybody can provide.
Thanks again,
Joel
Request Method: GET
Django Version: 2.2.7
Python Version: 3.7.0
Installed Applications:
['browse.apps.BrowseConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template E:\Python\Python37\lib\site-packages\django\contrib\admin\templates\admin\includes\fieldset.html, error at line 17
2
7 : <div class="form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% if not line.has_visible_field %} hidden{% endif %}{% for field in line %}{% if
field.field.name %} field-{{
field.field.name }}{% endif %}{% endfor %}">
8 : {% if line.fields|length_is:'1' %}{{ line.errors }}{% endif %}
9 : {% for field in line %}
10 : <div{% if not line.fields|length_is:'1' %} class="fieldBox{% if
field.field.name %} field-{{
field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>
11 : {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
12 : {% if field.is_checkbox %}
13 : {{ field.field }}{{ field.label_tag }}
14 : {% else %}
15 : {{ field.label_tag }}
16 : {% if field.is_readonly %}
17 : <div class="readonly"> {{ field.contents }} </div>
18 : {% else %}
19 : {{ field.field }}
20 : {% endif %}
21 : {% endif %}
22 : {% if field.field.help_text %}
23 : <div class="help">{{ field.field.help_text|safe }}</div>
24 : {% endif %}
25 : </div>
26 : {% endfor %}
27 : </div>
Traceback:
File "E:\Python\Python37\lib\site-packages\django\db\models\options.py" in get_field
565. return self.fields_map[field_name]
During handling of the above exception ('display_sample_image'), another exception occurred:
File "E:\Python\Python37\lib\site-packages\django\contrib\admin\utils.py" in lookup_field
262. f = _get_non_gfk_field(opts, name)
File "E:\Python\Python37\lib\site-packages\django\contrib\admin\utils.py" in _get_non_gfk_field
293. field = opts.get_field(name)
File "E:\Python\Python37\lib\site-packages\django\db\models\options.py" in get_field
567. raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
During handling of the above exception (SampleImage has no field named 'display_sample_image'), another exception occurred:
File "E:\Python\Python37\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "E:\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
145. response = self.process_exception_by_middleware(e, request)
File "E:\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
143. response = response.render()
File "E:\Python\Python37\lib\site-packages\django\template\response.py" in render
106. self.content = self.rendered_content
File "E:\Python\Python37\lib\site-packages\django\template\response.py" in rendered_content
83. content = template.render(context, self._request)
File "E:\Python\Python37\lib\site-packages\django\template\backends\django.py" in render
61. return self.template.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
171. return self._render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\loader_tags.py" in render
150. return compiled_parent._render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\loader_tags.py" in render
150. return compiled_parent._render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\loader_tags.py" in render
62. result = block.nodelist.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\loader_tags.py" in render
62. result = block.nodelist.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\defaulttags.py" in render
209. nodelist.append(node.render_annotated(context))
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\loader_tags.py" in render
188. return template.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
173. return self._render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\defaulttags.py" in render
209. nodelist.append(node.render_annotated(context))
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\defaulttags.py" in render
209. nodelist.append(node.render_annotated(context))
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\loader_tags.py" in render
188. return template.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
173. return self._render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\defaulttags.py" in render
209. nodelist.append(node.render_annotated(context))
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\defaulttags.py" in render
209. nodelist.append(node.render_annotated(context))
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\defaulttags.py" in render
309. return nodelist.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\defaulttags.py" in render
309. return nodelist.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in render
987. output = self.filter_expression.resolve(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in resolve
671. obj = self.var.resolve(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in resolve
796. value = self._resolve_lookup(context)
File "E:\Python\Python37\lib\site-packages\django\template\base.py" in _resolve_lookup
858. current = current()
File "E:\Python\Python37\lib\site-packages\django\contrib\admin\helpers.py" in contents
196. f, attr, value = lookup_field(field, obj, model_admin)
File "E:\Python\Python37\lib\site-packages\django\contrib\admin\utils.py" in lookup_field
271. value = attr(obj)
File "E:\Dropbox\Projects\popmusicplus\browse\admin.py" in display_sample_image
31. return obj.display_sample_image()
File "E:\Dropbox\Projects\popmusicplus\browse\models.py" in display_sample_image
138. height = width * self.sample_image.height / self.sample_image.width
File "E:\Python\Python37\lib\site-packages\django\core\files\images.py" in height
23. return self._get_image_dimensions()[1]
File "E:\Python\Python37\lib\site-packages\django\core\files\images.py" in _get_image_dimensions
28. self.open()
File "E:\Python\Python37\lib\site-packages\django\db\models\fields\files.py" in open
74. self.file = self.storage.open(
self.name, mode)
File "E:\Python\Python37\lib\site-packages\django\core\files\storage.py" in open
36. return self._open(name, mode)
File "E:\Python\Python37\lib\site-packages\django\core\files\storage.py" in _open
224. return File(open(self.path(name), mode))
Exception Type: FileNotFoundError at /admin/browse/sheet/3868/change/
Exception Value: [Errno 2] No such file or directory: 'E:\\Dropbox\\Projects\\popmusicplus\\images\\samples\\3868-awake-celeste-easy-piano_cover.png'