I have a project that gives the user an option to upload an avatar. The upload works fine and, in fact, I can see the avatar in the root /
of the project. However, when I navigate /anything_else/
, my project can't seem to find the avatar.
Below is the relevant part of the model that handles the upload:
avatar = models.ImageField('profile picture', upload_to='static/media/images/avatars/', null=True, blank=True)
In my user_base.html
template, I have the following line that shows the avatar
<img id="profile_pic" src="{{ user.avatar }}" alt="Profile picture" style="width:30px;height:30px;border:0">
So if I upload the picture guitar.jpg
and go to mysite.com/static/media/images/avatars/guitar.jpg
, it will be found.
However, if I navigate to mysite.com/news/
, I still expect to have a viewable avatar, but instead I get a 404. This is because it looks for the image at mysite.com/news/static/media/images/avatars/guitar.jpg
I've heard of template inheritance, but I'm not really sure how to do this. Any ideas?
EDIT
I solved it by changing
<img id="profile_pic" src="{{ user.avatar }}" alt="Profile picture" style="width:30px;height:30px;border:0">
to
<img id="profile_pic" src="/{{ user.avatar }}" alt="Profile picture" style="width:30px;height:30px;border:0">