Reputation: 2057
I want to attach and display an image in my django admin form. I have a model that gets used to create a matrix of fields that the user needs to understand how the mapping will work, and that is best described by using an image in the form.
I could do it in the frontend manually, but I want to keep it strictly in the admin. I can also overwride the admin form with a form that has custom widgets, but I'm not sure how to include an image from the sites static folder to get it into the admin form.
from django import forms
from django.contrib import admin
from .models import Buttons
class BTNForm(forms.ModelForm):
custom_widget = forms.TypedChoiceField(
choices = ((1, "Yes"), (0, "No")),
coerce = lambda x: bool(int(x)),
widget = forms.RadioSelect,
initial = '1',
required = True,)
class Meta:
model = Buttons
class ButtonAdmin(admin.ModelAdmin):
form = BTNForm
How to go about it?
Upvotes: 0
Views: 98
Reputation: 479
If you really need to do it and you are using ModelForm you can pass it as helptext. Simply add to your meta, under correct fieldname:
help_texts = {
'FieldName': ('<img src="X" height="XXX"/>'),
}
Upvotes: 1