0

i'm having trouble displaying images on my Djnago admin page.

i read https://stackoverflow.com/questions/2443752/how-to-display-uploaded-images-in-change-list-page-in-django-admin/51181825#51181825 this article but still didn't get what i want :(

this is my codes

models.py

from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser, PermissionsMixin
)
from django.db import models
from django.utils.html import mark_safe


class User(AbstractBaseUser, PermissionsMixin):
    image = models.ImageField(
        upload_to='media/profile_image/',
        null=True
    )


admin.py

class UserAdmin(BaseUserAdmin):
    # The forms to add and change user instances
    form = UserChangeForm
    add_form = UserCreationForm

    def image_tag(self, obj): # Here
        return format_html(
            f'''<a href="{obj.image.url}" target="_blank">
                  <img 
                    src="{obj.image.url}" alt="{obj.image}" 
                    width="50" height="50"
                    style="object-fit: cover;"
                  />
                </a>''')


    list_display = ('uid','get_full_name', 'email', 'nickname', 'introduce','image', 'birth','is_active', 'is_superuser', 'date_joined', 'image_tag')

...

this is what i got

admin page

the image is in my PROJECTNAME/media/profile_image but i cant' display it on my admin page :(

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
skay138
  • 3
  • 1
  • Show settings.py part related to media files and urls.py – Ivan Starostin Jan 17 '23 at 07:32
  • thank you so much for your comment, but i solved this by adding if settings.DEBUG: # new urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) this on my urls.py sorry for not searching docs as possible and asking... :( – skay138 Jan 17 '23 at 07:49

1 Answers1

0

I solved the problem by adding

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

this on my urls.py

sorry for asking what i could solve it my self, but I will left this for someone who might have same problem... thanks

skay138
  • 3
  • 1