views:

34

answers:

2

Hi,

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  A: 

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.

Mark Lavin
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
got it: {{ original.xxx }} thank you!
schneck
+1  A: 

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.

fish2000
I know this technique, but it only works for lists, not detail pages.
schneck