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

1

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 models.py

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)

administrator views.py

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

2 answers

2

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:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

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
source
1

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