1

I am trying to display an image from online source (e.g. facebook) which is not a field in the model in the User Change Form.

I can successfully display it in the user list page, or in a separate Model page, or in the add new user page, but I want to add this image to the existing user editing page below username and password fields.

enter image description here

class TestForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('email', 'password')

@admin.register(User)
class UserAdmin(BaseUserAdmin):
      form = TestForm

      # ... attributes

Nothing happened even it's a test form with 2 defined model fields. I expected the page to be showing email and password only.

# User Model
def image(self):
    return mark_safe('<img src="http://onlinesource.com/image.png" width=200 height=200 alt="image" />')
image.short_description = _('Image')

# admin.py
@admin.register(User)
class UserAdmin(BaseUserAdmin):
    # ...
    list_display = ('image',) # this works and you can see the image in list view page
    fieldsets = (None, {"fields": ("image",)}) # this will throw error since `image` is not a field

This is basically to display image in the list view page. It can't be shown in single user page as image is not a model field.

legenddaniel
  • 698
  • 2
  • 9
  • 17
  • Does this answer your question? [Django Admin Show Image from Imagefield](https://stackoverflow.com/questions/16307307/django-admin-show-image-from-imagefield) – Mihai Chelaru Oct 22 '21 at 00:32
  • @MihaiChelaru I think this is for showing image in the list view of users instead of the single user page – legenddaniel Oct 22 '21 at 00:36
  • The very first sentence of that question: *While I can show an uploaded image in list_display is it possible to do this on the per model page (as in the page you get for changing a model)?* So it does appear that the OP there is specifically referring to the same editing page for existing objects. Have you tried the solution there with hardcoding this external image URL you're referring to? – Mihai Chelaru Oct 22 '21 at 01:36
  • @MihaiChelaru I am pretty sure all 12 answers there did not answer my question. I would like to display an image which is NOT a model field in the SINGLE USER PAGE. All they did are displaying image in list view (set `list_display`), or displaying image from ImageField, or completely rewriting the template – legenddaniel Oct 22 '21 at 19:53
  • As I explained in my comment, have you tried doing what they suggest only **hardcoding** the image URL you have instead of pulling it in from the model? What's wrong with modifying the template? Not sure where you got the idea to use `list_display` from, as the accepted answer on that question is adding to `fields`. – Mihai Chelaru Oct 22 '21 at 20:33
  • @MihaiChelaru First, I can't directly render anything inside a class in `admin.py` like any html segment or custom html template. Second, since the `BaseUserAdmin` has defined `fieldsets` so I can't use `fields`. Third, this image is not a field defined by `models.ImageField`, if you add `image` to the `fields` or `fieldsets` tuple you will get error. Please check my update. – legenddaniel Oct 22 '21 at 21:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238442/discussion-between-legenddaniel-and-mihai-chelaru). – legenddaniel Oct 22 '21 at 22:00

1 Answers1

0

Refer to this post. https://stackoverflow.com/a/16307554/21436748

# User Model
def image(self):
    return mark_safe('<img src="http://onlinesource.com/image.png" width=200 height=200 alt="image" />')
image.short_description = _('Image')

# admin.py
@admin.register(User)
class UserAdmin(BaseUserAdmin):
    # ...
    list_display = ('image',) # this works and you can see the image in list view page

    ##### Add image to read-only field tuple ######
    readonly_fields = ('image',)

    fieldsets = (None, {"fields": ("image",)}) 

From Django 1.8, image tag 'image' only in readonly_fields will not display. The image tag 'image' only in fields gives an error of an unknown field, as you mentioned above.

You need the image tag 'image' both in fields and in readonly_fields in order to display correctly.

Jithendra
  • 1
  • 2