Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question