settings.py
add lines:
import os BASE_DIR = os.path.realpath(os.path.dirname(__file__))
replace strings:
MEDIA_ROOT = '' MEDIA_URL = ''
from
MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR,os.pardir,'media')
this should set up your project to display media content from the folder / of your project directory / media /
urls.py
also add the line:
import settings
add the following line to url patterns:
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),
models.py
inside your model add the following line:
File = models.FileField('File',upload_to='./')
define a method in a model
def fileLink(self): if self.File: return '<a href="' + str(self.File.url) + '">' + 'NameOfFileGoesHere' + '</a>' else: return '<a href="''"></a>' fileLink.allow_tags = True fileLink.short_description = "File Link"
admin.py
use the fileLink
field as a read field, you can also add it to your list_display
eg,
class FileAdmin(admin.ModelAdmin): list_display = ['fileLink'] readonly_fields = ['fileLink']
source share