Working with images in Django 1.9, my avatar is not shown in all my views


When I'm in a view other than editing users or creating users, it does not show me the profile image.

Here when I'm in any of the views I have, it does not show me its profile picture example: view on products Source code:

<img src='' alt="jorgelca2"  
  class="circle responsive-img valign profile-image">

This is when I go in to edit a user or to register if he shows me the profile picture

source code:

<img src='/media/avatars/O3EJ9J0.jpg' alt="jorgelca2"  class="circle responsive-img valign profile-image">

my base.html which is that all the views entangle the design. so I call the image

<img src='{{ object.avatar.url }}' 
    alt="{{ user.username }}"  
    class="circle responsive-img valign profile-image">

I'm extending to user

class Usuario(AbstractUser):
telefono = models.CharField('Télefono', max_length=15)
avatar = models.ImageField('avatar para tu perfil', upload_to='avatars/', blank=True, null=True)
fondo = models.ImageField('Elige tu fondo de perfil', upload_to='fondos/', blank=True, null=True)


class CodeaAdminView(LoginRequiredMixin, TemplateView):
template_name = 'codea_admin/codea_admin.html'
asked by wootsbot 22.04.2016 в 23:30

2 answers


What happens is that the view in the URL producto/nuevo does not have the object object which I suppose is the user (you do not show that part of the code).

But the object user always is in the request if you use the request module in the context processors of your templates.

Make sure you have that module 'django.template.context_processors.request' in your configuration:

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [

And then you can use all your templates this:

<img src='{{ request.user.avatar.url }}' 
    alt="{{ request.user.username }}"  
    class="circle responsive-img valign profile-image">
answered by 24.04.2016 / 00:07

It seems to me that you rewrite the object object and it is no longer a user model instance or it should be user.avatar.url

answered by 23.04.2016 в 23:15