Although there are already some good, functional solutions here, I believe that informal markup, such as auxiliary image tags, belongs to templates, and is not tied to widgets of Django forms or generated in model administrative classes. More semantic solution:
Admin Template Override
Note. Obviously, my reputation is not high enough to post more than two simple links, so I created annotations in the following text and included the corresponding URLs at the bottom of this answer.
From the documentation of the Django admin site :
It is relatively easy to override many of the templates that the admin module uses to create various admin site pages. You can even override some of these templates for a specific application or model.
Django django.contrib.admin.options.ModelAdmin (usually accessed in the django.contrib.admin.ModelAdmin namespace) represents a series of possible template paths for the Django template loader in order from most specific to less. This snippet was copied directly from django.contrib.admin.options.ModelAdmin.render_change_form :
return TemplateResponse(request, form_template or [ "admin/%s/%s/change_form.html" % (app_label, opts.model_name), "admin/%s/change_form.html" % app_label, "admin/change_form.html" ], context)
Therefore, given the aforementioned documentation on overriding Django admin templates and the search path for templates, suppose you have created the "Articles" application, which defines the "Article" model class. If someone wants to override or expand only the standard form for changing the Django admin site for the articles.models.Article model, you can follow these steps:
- Create a template directory structure for the override file.
- Although this is not mentioned in the documentation, the template loader will first search the application directories if
APP_DIRS 1 is set to True . - Since someone wants to redefine the Django admin site template by application label and model, the resulting directory hierarchy will look like this:
<project_root>/articles/templates/admin/articles/article/
- Create a template file in one new directory structure.
- Only the admin change form should be overridden, so create
change_form.html . - The final absolute path will be
<project_root>/articles/templates/admin/articles/article/change_form.html
- Completely override or simply expand the default admin change form template.
- I could not find any information in the Django documentation regarding contextual data available for standard admin site templates, so I was forced to take a look at the source code for Django.
- Default change form template: github.com/django/django/blob/master/django/contrib/admin/templates/admin/change_form.html
- Several relevant context dictionary definitions can be found in
django.contrib.admin.options.ModelAdmin._changeform_view and django.contrib.admin.options.ModelAdmin.render_change_form
My decision
Assuming that my name for the ImageField attribute in the model is โfileโ, my overriding the template to implement image preview would be something like this:
{% extends "admin/change_form.html" %} {% block field_sets %} {% if original %} <div class="aligned form-row"> <div> <label>Preview:</label> <img alt="image preview" src="/{{ original.file.url }}" style="max-height: 300px;"> </div> </div> {% endif %} {% for fieldset in adminform %} {% include "admin/includes/fieldset.html" %} {% endfor %} {% endblock %}
original represents the instance of the model from which ModelForm was generated. Also, I usually donโt use inline CSS, but it didnโt cost a separate file for one rule.
Sources:
- docs.djangoproject.com/en/dev/ref/settings/#app-dirs