0

I have a product model that has an image_name property.

image_name = models.CharField(max_length=500)

I would like to store inside a static image name such as "super_product.jpg" for example.

In my product view, I am sending the full object as context.

def product_detail_view(request, id):
    obj = get_object_or_404(Product, id=id)
    context = {
        'object': obj
    }
    return render(request, "products/product_detail.html", context)

The problem is in my html. I tried to do this way:

<img src="{% static {{ object.image_name }} %}" />

But it doesn't seem possible to use static and object property this way.

Does anyone know a way to do it? Thanks in advance!

Rafael
  • 1
  • Does this answer your question? [django 1.5 - How to use variables inside static tag](https://stackoverflow.com/questions/16655851/django-1-5-how-to-use-variables-inside-static-tag) – Davit Tovmasyan Jan 21 '20 at 07:59

1 Answers1

0

In settings.py:

STATIC_URL = '/static/'

In template file:

<img src="{% static object.image_name %}" />

You wrote <img src="{% static {{ object.image_name }} %}" /> {% static 'path.image'%} that is the convention so you do not need to add extra curly braces to show the image name.

Iqbal Hussain
  • 1,077
  • 1
  • 4
  • 12