A
A
apheyhys2018-10-13 15:49:47
Django
apheyhys, 2018-10-13 15:49:47

Why are thumbnails not showing up in the Django admin?

Only the html tag is displayed.
5bc1e8fb43af2843185160.pngsetting.py

MEDIA_ROOT = '/home/apheyhys/PycharmProjects/jhchase/media/'
MEDIA_URL = '/media/'

urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('novels.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

models.py
class Cover(models.Model):
    name_cover = models.CharField(max_length=100, blank=True, null=True)
    cover = models.ImageField()

    def image_img(self):
        return u'<img src="%s" />' % str(self.cover.url)
    image_img.short_description = 'Изображение'
    image_img.allow_tags = True

admin.py
class CoverAdmin(admin.ModelAdmin):
    list_display = ("cover", "image_img")
    readonly_fields = ("image_img",)

admin.site.register(Cover, CoverAdmin)

Maybe someone faced the same?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2018-10-14
@fox_12

Try something like this:

from django.utils.safestring import mark_safe
...
   def image_img(self):
        return mark_safe(u'<img src="%s" />' % str(self.cover.url))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question