1

I have uploaded some files in django admin using

File = models.FileField(upload_to='./list/') 

Now I want to download the same file for which I have written the below function within the class :

def file_link(self):
    request = None
    full_url = ''.join(['http://', get_current_site(request).domain, self.cvFile.url])
    if self.cvFile:
        return "<a href='%s'>download</a>" % (full_url)
    else:
        return "No attachment"

file_link.allow_tags = True

However when I click on the link, it redirects to a page with the link appended to the present url. And since the url doesn't exist, it is showing error.

harsh sahay
  • 11
  • 1
  • 4

2 Answers2

2

you can simply do this

Click to get the code

from django.contrib import admin
from app.models import *

class AppAdmin(admin.ModelAdmin):
    list_display = ('author','title','file_link')
    def file_link(self, obj):
        if obj.file:
            return "<a href='%s' download>Download</a>" % (obj.file.url,)
        else:
            return "No attachment"
    file_link.allow_tags = True
    file_link.short_description = 'File Download'

admin.site.register(AppModel , AppAdmin)
Community
  • 1
  • 1
Basil Jose
  • 1,004
  • 11
  • 13
0

You need an absolute url inside href, whereas self.cvFile.url will return a relative url. So unless you are on the home page, the link will simply append on to whatever url you are currently on. Simply add the domain url to self.cvFile.url and it should work.

TheGeorgeous
  • 3,927
  • 2
  • 20
  • 33