это код в views.py
def store (request):
products = {
'products':product.objects.all()
}
return render (request, 'neizer/products.html', context = products)
это шаблон
{% block content %}
<div class="products-parent">
{%for product in products%}
<div class="product-child">
<img src={{product.image.url}} alt="no image available" />
<h2>{{product.name}}</h2>
<button id="learn-more-btn">learn more</button>
<p>${{product.price}} <i class="fas fa-cart-plus"></i></p>
</div>
{% endfor %}
</div>
{% endblock content %}
Я загрузил изображения с помощью интерфейса / admin, когда я получаю доступ к URL-адресу изображения в django shell Я получаю фактический URL (который открывается в браузере с изображением), но когда я пытаюсь получить к нему доступ в шаблоне, я получаю вышеуказанную ошибку, проблема, как кажется, связана с product.image.url
, как-то django, похоже, не правильно понимает экземпляр класса. это обратная связь:
Traceback (most recent call last):
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 828, in _resolve_lookup
current = current[bit]
During handling of the above exception ('ImageFieldFile' object is not subscriptable), another exception occurred:
File "/mnt/s/env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/mnt/s/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/mnt/s/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/mnt/s/webapp/main/views.py", line 20, in store
return render (request, 'neizer/products.html', context = products)
File "/mnt/s/env/lib/python3.6/site-packages/django/shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/defaulttags.py", line 209, in render
nodelist.append(node.render_annotated(context))
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 986, in render
output = self.filter_expression.resolve(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 670, in resolve
obj = self.var.resolve(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 795, in resolve
value = self._resolve_lookup(context)
File "/mnt/s/env/lib/python3.6/site-packages/django/template/base.py", line 836, in _resolve_lookup
current = getattr(current, bit)
File "/mnt/s/env/lib/python3.6/site-packages/django/db/models/fields/files.py", line 61, in url
self._require_file()
File "/mnt/s/env/lib/python3.6/site-packages/django/db/models/fields/files.py", line 38, in _require_file
raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
Exception Type: ValueError at /store
Exception Value: The 'image' attribute has no file associated with it.