djangodjango-modelsuploaddjango-templates

Django Admin Inline FileField link to open in new tab


I have fieldfields in an inline in Django Admin. The link to the file works fine and the uploaded file is shown in browser. Now i want to make the link open in a new tab. I imagine the quickest way is to insert a target="_blank" into the html, but there seems to be no template for it - maybe it's generated by python code. Can you please help me pinpoint where to inject?

models.py:

class Anhang(models.Model):
   antrag = models.ForeignKey(Antrag)
   upload = models.FileField(upload_to=subfolderpath)

admin.py:

class AnhangInline(admin.TabularInline):
   model = Anhang
   extra = 0
   show_change_link = False

In Admin:

enter image description here

Thank you very much.

Solution, thanks to @JulienS:
1. Override the template in your app:
django/contrib/admin/templates/admin/widgets/clearable_file_input.html
2. Insert in template:
<a target="_blank" href="{{ widget.value.url }}">


Solution

  • I think you find a widget to URL on this django\contrib\admin\templates\admin\widgets where you instal your django.