B
B
bituke2020-09-04 07:57:51
Django
bituke, 2020-09-04 07:57:51

How to display an image in a django template from a model?

This is a stml document from janga:
catalog.html

{% block catalog %}
{% for a in latest_product %}
  <p>{{a.product_name}} - {{a.product_price}} рублей</p>
  <img src="{% static '{{a.product_img}}' %}">

In the loop, you need to display the image from the model (that is, from the database)
That is, to the name of the product, you need to display the image of this product, which the user will download.
Here is the model code:

models.py
class Product(models.Model):
  product_name = models.CharField('Название товара', max_length=50)
  product_img = models.ImageField('Обложка товара', upload_to='static/imgproduct/')
  product_price = models.IntegerField('Цена товара', default=0)
  def __str__(self):
    return self.product_name

  class Meta:
    verbose_name = 'Товар'
    verbose_name_plural = 'Товары'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-09-04
@bituke

1. Insert the code correctly, there is a code tag for this
2. Read the rules on how to put tags to the question
3. Well, read the docs more carefully, there is definitely nothing about such a use of static, you just need to<img src="{{ a.product_img.url }}">

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question