initall
initall

Reputation: 2385

Django: Correct way to extend a certain field in the admin change form with HTML?

Let's say I have a model with a field based on the ImageField class.

class Foo(models.Model):
    imagefile = models.ImageField('File', upload_to='foo/%Y/%m%/%d/')

Django adds - after first upload - an input tag of type file to let me change it and a link to the image to view it.

I want this original field specific (HTML) code as is (and by no means create it myself manually) but also add other HTML/JS code, say to include a thumbnail-preview or add some AJAX-stuff. I can image a few other use cases for other fields, too.

What's the correct (say: easy/unobtrusive) way to implement something like that?

Upvotes: 0

Views: 376

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599590

You need to write a custom widget. Look at django.forms.widgets for the code of the existing FileInput widget, which you can subclass and override the render method where necessary. You'll then just need to assign that widget for your file field in your admin form.

Upvotes: 2

Related Questions