By default, when use Django admin to upload a file/image in inline models, the url it got is:
Currently: ct_attachment/2014/09/18/cloud_SL4WKba.jpg
Change:
When one click the URL to download the file already uploaded, it will open in the same window, which means the admin UI will be replaced with the file downloading window.
Sometimes, we just need to check what’s the file it is, so open it in a new windows is much better.
Add
is enough, but how to add it?target=_blank
` in
`<a href=....
Forunately, there is a
variable can be overrided in the AdminModel.formfield_overrides
By checking django source codes, if we define a customized widget, override the url_markup_template variable which defined in django.forms.ClearableFileInput, then we can do a trick to implement this change, e,g:
class FileInNewWindowWidget(admin.widgets.AdminFileWidget):
# AdminFileWidget inherits from django.forms.ClearableFileInput
# The original url_markup_template in django.forms.ClearableFileInput is:
# url_markup_template = '{1}'
url_markup_template = '{1}'
class AttachmentInline(admin.TabularInline):
formfield_overrides = {
models.FileField: {'widget': FileInNewWindowWidget},
}
class CustomerAdmin(admin.ModelAdmin):
inlines = [AttachmentInline]