Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Display image in admin interface

I've defined a model which contains a link an image. Is there a way to display the image in the model items list? My model looks like this:

class Article(models.Model):     url = models.CharField(max_length = 200, unique = True)     title = models.CharField(max_length = 500)     img = models.CharField(max_length = 100) # Contains path to image      def __unicode__(self):        return u"%s" %title 

Is there a way to display the image together with title?

like image 808
Oleg Tarasenko Avatar asked Mar 14 '10 20:03

Oleg Tarasenko


People also ask

How do I display an image in Django?

How you specify the location of an image in Django is in between {% %}. In between these brackets, you specify static 'images\\Python. png', where Python is the image you want to display which is inside of the images directory in the static directory you create for the current app you are in.


1 Answers

You can create a model instance method with another name, allow HTML tags for its output and add this method as a list field. Here is an example:

First add a new method returning the HTML for the image inclusion:

class Article(models.Model):     ...     def admin_image(self):         return '<img src="%s"/>' % self.img     admin_image.allow_tags = True 

Then add this method to the list:

class ArticleAdmin(admin.ModelAdmin):         ...     list_display = ('url', 'title', 'admin_image') 
like image 196
Michael Avatar answered Oct 24 '22 00:10

Michael