【发布时间】:2015-04-14 19:25:36
【问题描述】:
我有一个项目,让用户可以选择上传头像。上传工作正常,事实上,我可以在项目的根/ 中看到头像。但是,当我导航/anything_else/ 时,我的项目似乎找不到头像。
下面是模型中处理上传的相关部分:
avatar = models.ImageField('profile picture', upload_to='static/media/images/avatars/', null=True, blank=True)
在我的user_base.html 模板中,我有以下显示头像的行
<img id="profile_pic" src="{{ user.avatar }}" alt="Profile picture" style="width:30px;height:30px;border:0">
所以如果我上传图片guitar.jpg,然后去mysite.com/static/media/images/avatars/guitar.jpg,就会被找到。
但是,如果我导航到 mysite.com/news/,我仍然希望有一个可见的头像,但我得到的是 404。这是因为它在 mysite.com/news/static/media/images/avatars/guitar.jpg 查找图像
我听说过模板继承,但我不确定如何做到这一点。有什么想法吗?
编辑
我通过改变解决了它
<img id="profile_pic" src="{{ user.avatar }}" alt="Profile picture" style="width:30px;height:30px;border:0">
到
<img id="profile_pic" src="/{{ user.avatar }}" alt="Profile picture" style="width:30px;height:30px;border:0">
【问题讨论】:
标签: django django-templates avatar