[英]Django - {{ MEDIA_URL }} not working as expected
I'm having a slight problem where my Django media tag isn't working properly. 我在Django媒体代码无法正常工作时遇到了一个小问题。
This doesn't return the path I expect - which would be ' /media/uploads/image_name_goes_here/
', and instead I get ' /uploads/image_name_goes_here/
' even though I have specified in my settings.py that the MEDIA_URL = '/media/'
. 这不返回我期望的路径-这将是“
/media/uploads/image_name_goes_here/
”,而是我得到“ /uploads/image_name_goes_here/
”即使我在settings.py已指定的MEDIA_URL = '/media/'
。
My upload_to
path in the models for the Image is the following function.. 我在Image模型中的
upload_to
路径是以下函数。
def img_path(instance, filename):
return ('uploads/%s' % (filename))
class Image(models.Model):
...
image_url=models.ImageField(upload_to=img_path,null=True)
...
My html: <img src='{{ MEDIA_URL }}/{{ Image.img_path }}'/>
我的html:
<img src='{{ MEDIA_URL }}/{{ Image.img_path }}'/>
Any ideas on why the 'media/' bit of the URL does not show up? 关于为什么URL的“ media /”位不显示的任何想法?
You don't have MEDIA_URL in the context of your template; 您的模板上下文中没有MEDIA_URL; it's not there by default.
默认情况下不存在。
Note however that you don't need it, and you shouldn't be accessing the field like this. 但是请注意,您不需要它,并且不应该这样访问该字段。 Use the
url
attribute: 使用
url
属性:
<img src='{{ Image.img_url.url }}'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.