Django Admin: Displaying Images in Your Models

Jonathan Hoffman
Django Unleashed
Published in
2 min readJan 29, 2024

Managing and displaying images efficiently is a pivotal aspect of many web applications. If you’re using Django, the admin interface provides a powerful, yet straightforward way to interact with your data models, including those handling images. In this blog post, we’ll walk you through the steps to show images in the Django Admin interface, ensuring your admin panel is both functional and visually appealing.

Step 1: Define Your Image Model in models.py

First, let’s define our Image model. This model is straightforward, consisting of a name and an image field:

# models.py
from django.db import models

class Image(models.Model):
name = models.CharField(max_length=250)
image = models.ImageField(upload_to='images/')

def __str__(self):
return self.name

In this snippet, ImageField is used for storing images, with upload_to='images/' determining the folder where uploaded images will be stored.

Step 2: Customize Image Representation in admin.py

Next, we want to ensure that these images are not just stored effectively but are also displayed elegantly in the Django Admin. We do this by customizing the ImageAdmin class:

# admin.py
from django.contrib import admin
from .models import Image
from django.utils.html import format_html

class ImageAdmin(admin.ModelAdmin):

def image_tag(self, obj):
return format_html('<img src="{}" style="max-width:200px; max-height:200px"/>'.format(obj.image.url))

image_tag.short_description = 'Image'
list_display = ['name', 'image_tag',]

admin.site.register(Image, ImageAdmin)

In the ImageAdmin class, image_tag method is defined to render the image in the admin list view. format_html is used to safely render HTML in Django. The list_display property ensures that both the name and the image are displayed in the admin list page.

Step 3: Ensure Your Media Files are Accessible in urls.py

Lastly, it’s crucial to ensure your project’s URL configuration is aware of the media files. This is particularly important during development, as Django does not serve media files by default:

# project/urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path

urlpatterns = [
...
# your urlpatterns
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Adding + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) to your urlpatterns makes your uploaded media accessible during development.

With these steps, you’ve successfully configured your Django project to not only store images efficiently but also display them directly in the admin interface, enhancing the manageability and visual appeal of your admin panel. As you integrate these functionalities, remember that the way you manage media files in production may differ, so consider your hosting and storage options carefully for a live environment.

Hope this was useful! Happy coding!

--

--