2

I have a ModelAdmin where I need to insert some html-snippet that is not part of a model (it's a java-applet). Is there any way to do this?

2 Answers 2

2

You have a couple options. If the applet is related to one of the form fields then you could create a custom widget which includes the applet. Another way would be to override the template used by the model change form and include the applet. The template should be in admin/app_name/model_name/change_form.html in your templates directory where app_name and model_name are replaced by the appropriate values for your model.

1
  • hi, thanks for your suggestion, i think the second one will work for me. Only one question: Ho can I access the object in the template? e.g. {{ object.id}} does not work
    – schneck
    Mar 19, 2010 at 17:24
1

I tend to do a lot of this sort of thing, which is pretty much what you seem to want:

class SomeModelAdmin(admin.ModelAdmin):
    ...
    list_display = (
        'visible',
        'thumbnail',
        'size',
        'url',
    )
    ...

    def thumbnail(self, obj):
        return u'<img src="%s" />' % obj.url

    thumbnail.allow_tags = True

... et voila, ad-hoc HTML snippets. obj is the model instance in question. Personally I find this more flexible than endlessly subclassing Widgets, ModelForms et al -- your mileage may vary depending on what you do with the admin site, or if your're of the more orthodox object-oriented persuasion; it's helpful to know how to do it in any case.

1
  • 2
    I know this technique, but it only works for lists, not detail pages.
    – schneck
    Mar 21, 2010 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.