问题
i'm trying to access files that were uploaded by users.
i have the following settings:
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'project/media/')
MEDIA_URL = '/project/media/'
and the file are:
path = models.FileField(upload_to="myApp")
image = models.ImageField(upload_to="myApp/logos", null=True, blank=True)
in the template i try to acces it in the following way:
{{MEDIA_URL}}{{file.path}}
but it doesn't work..
what am i doing wrong?
回答1:
file.path
returns an object representing the file, what you want is file.path.url
which takes care of adding the MEDIA_URL
.
来源:https://stackoverflow.com/questions/30012204/django-using-media-url-in-a-template