django在模板中使用MEDIA URL [英] django using MEDIA URL in a template

查看:530
本文介绍了django在模板中使用MEDIA URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问用户上传的文件。

i'm trying to access files that were uploaded by users.

我有以下设置:

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'project/media/') 

MEDIA_URL = '/project/media/'

,文件是:

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}}

但它

我做错了什么?

推荐答案

p> file.path 返回一个表示文件的对象,你想要的是 file.path.url 添加 MEDIA_URL

file.path returns an object representing the file, what you want is file.path.url which takes care of adding the MEDIA_URL.

这篇关于django在模板中使用MEDIA URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆