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?
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With