S
S
Stanislav Shabalin2020-08-27 21:40:22
Django
Stanislav Shabalin, 2020-08-27 21:40:22

How to display an image in the admin panel of a post?

Good evening !

I want to see posts in the admin panel not only as a list of titles, but also display images. How is this implemented in Django?
While there is not enough experience to figure it out yourself.

Here is the model:

class Article(models.Model):
  category = models.ForeignKey('Category', on_delete=models.SET_NULL, null=True, blank=False)
  slug = models.SlugField('Slug', max_length=150, unique=True, null=True, blank=True)
  title = models.CharField('Название статьи', max_length=150, help_text='Название статьи в текущем разделе')
  content = models.TextField('Описание статьи', db_index=True, null=True, blank=True)
  image = models.ImageField('Главное изображение', upload_to='articles/', null=True, blank=True)

  def image_img(self):
    if self.image:
      return u'<a href="{0}" target="_blank"><img src="{0}" width="100"/></a>'.format(self.image.url)
    else:
      return '(Нет изображения)'
    image_img.short_description = 'Фото'
    image_img.allow_tags = True

    def __str__(self):
        return self.title


It seems that everything is added, but the photo does not appear. Do I need to add something to the admin?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-08-27
@Starck43

Read the docs, there is actual info https://docs.djangoproject.com/en/3.1/ref/utils/#d... and allow_tags has long been drank

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question