[PYTHON] How to view images in Django's Admin

On the Django management site, there was a case that "If the image can be displayed on the list screen, it would be good work efficiency", but when I checked it, it was very easy, so I will record it as Tips.

I'm a little sorry that Qiita has only a little simple information, but Django is often filled with Stack Overflow even if you do a Google search, so there is little information that hits in Japanese, so it is useful for someone I think it may be posted.

For this article, we have confirmed the operation with Django 1.8.

What you want to do

イメージ図

How to write

I'll write it as a continuation of my book How to use ManyToManyField with Django's Admin.

As shown below, define a method (ʻadmin_og_image) with an arbitrary name that returns the file path of the image in models.py, and set ʻallow_tags = True.

models.py

@@ -11,9 +11,18 @@ class Article(models.Model):
     title = models.CharField(max_length=100)
     author = models.ForeignKey(Author, blank=True, null=True)
     meta = models.ManyToManyField('Tag')
-
+    og_image = models.ImageField(default='')
+    
     def __unicode__(self):
         return self.title
+    
+    def admin_og_image(self):
+        if self.og_image:
+            return '<img src="{}" style="width:100px;height:auto;">'.format(self.og_image)
+        else:
+            return 'no image'
+        
+    admin_og_image.allow_tags = True

Then add the method name to list_display in admin.py and you're done.

admin.py

@@ -3,7 +3,7 @@ from django.contrib import admin
 from .models import Author, Article, Tag
 
 class ArticleAdmin(admin.ModelAdmin):
-    list_display = ('title', 'author', '_meta')
+    list_display = ('title', 'author', '_meta', 'admin_og_image')

Supplement

In addition, it is also possible to write it completely with only admin.py, in which case it is OK if you write as follows.

models.py

@@ -11,9 +11,18 @@ class Article(models.Model):
     title = models.CharField(max_length=100)
     author = models.ForeignKey(Author, blank=True, null=True)
     meta = models.ManyToManyField('Tag')
-
+    og_image = models.ImageField(default='')
+    

admin.py

@@ -3,11 +3,19 @@ from django.contrib import admin
 from .models import Author, Article, Tag
 
 class ArticleAdmin(admin.ModelAdmin):
-    list_display = ('title', 'author', '_meta')
+    list_display = ('title', 'author', '_meta', 'admin_og_image')
 
     def _meta(self, row):
         return ','.join([x.name for x in row.meta.all()])
 
+    def admin_og_image(self, row):
+        if row.og_image:
+            return '<img src="{}" style="width:100px;height:auto;">'.format(row.og_image)
+        else:
+            return 'no image'
+        
+    admin_og_image.allow_tags = True
+    

Recommended Posts

How to view images in Django's Admin
How to collect images in Python
How to use ManyToManyField with Django's Admin
How to implement Scroll View in pythonista 1
How to draw OpenCV images in Pygame
View images in Matplotlib
How to specify a schema in Django's database settings
How to display multiple images of galaxies in tiles
How to use bootstrap in Django generic class view
How to use the exists clause in Django's queryset
How to use Django's GeoIp2
How to upload files in Django generic class view
How to develop in Python
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
How to use classes in Theano
How to write soberly in pandas
How to update Spyder in Anaconda
How to reflect CSS in Django
How to kill processes in bulk
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to run TensorFlow 1.0 code in 2.0
How to handle Japanese in Python
How to log in to Docker + NGINX
How to call PyTorch in Julia
How to smartly define objects that are commonly used in View
How to use calculated columns in CASTable
[Introduction to Python] How to use class in Python?
How to suppress display error in matplotlib
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
How to delete expired sessions in Django
[Itertools.permutations] How to put permutations in Python
How to use Google Test in C
How to implement nested serializer in drf-flex-fields
How to execute commands in jupyter notebook
How to do'git fetch --tags' in GitPython
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to reassign index in pandas dataframe
How to check opencv version in python
How to enable SSL (TLS) in Apache
How to use Anaconda interpreter in PyCharm
How to specify non-check target in Flake8
How to switch python versions in cloud9
How to use __slots__ in Python class
How to collect face images relatively easily
How to dynamically zero pad in Python
How to do Server-Sent Events in Django
How to use regular expressions in Python
How to convert DateTimeField format in Django
How to use Map in Android ViewPager
How to add options to Django's manage.py runserver
How to display Hello world in python
How to read CSV files in Pandas
How to change editor color in PyCharm