django using MEDIA URL in a template

爷,独闯天下 提交于 2019-12-11 11:14:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!