0

I have legacy software that stores the institutions logo through django's BinaryField, I need to show this image on the django admin record editing page

this my admin:

class InstituicaoAdmin(admin.ModelAdmin):
    list_display = ['ds_instituicao', 'sigla']
    search_fields = ['ds_instituicao']
    formfield_overrides = {
        BinaryField: {'widget': BinaryFileInput()},
    }
    readonly_fields = ["imagem_logo",]

    def imagem_logo(self, obj):
        base64Encoded = base64.b64encode(obj.logo)
        return mark_safe('<img src="data:;base64,base64Encoded">')

This not work

I want some like this: Image_FIeld

GustavoNogueira
  • 389
  • 1
  • 3
  • 16
  • You might find answers to this Question useful https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html – Azizur Rahman Nov 13 '19 at 20:52

1 Answers1

0

I would use format_html instead of mark_safe:

    def imagem_logo(self, obj):
        base64Encoded = base64.b64encode(obj.logo)
        return format_html('<img src="data:;base64,{}">', base64Encoded)

See the docs at: https://docs.djangoproject.com/en/2.2/ref/utils/#django.utils.html.format_html

azundo
  • 5,902
  • 1
  • 14
  • 21