How to make the new uploaded file in Django admin UI to open in a new window

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 target=_blank` in `<a href=.... is enough, but how to add it?

Forunately, there is a formfield_overrides variable can be overrided in the AdminModel.
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]
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇