# how to get profile pic in navbar from logged in user to display

**URL:** <https://forum.djangoproject.com/t/how-to-get-profile-pic-in-navbar-from-logged-in-user-to-display/35044>\
**Category:** Using Django\
**Created:** [September 23, 2024, 4:57pm UTC](https://forum.djangoproject.com/t/how-to-get-profile-pic-in-navbar-from-logged-in-user-to-display/35044 "2024-09-23T16:57:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![skeetno1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/skeetno1/32/23527_2.png) [@skeetno1](https://forum.djangoproject.com/u/skeetno1)\
**Post date:** [September 23, 2024, 4:57pm UTC](https://forum.djangoproject.com/t/how-to-get-profile-pic-in-navbar-from-logged-in-user-to-display/35044/1 "2024-09-23T16:57:07Z")

</div>

I’m trying to get the profile pic that the user uploaded in their profile to display in the navbar. This is dynamic as the user can change their profile pic. I can’t seem to figure out the template to make this work … here are some snippets…

the uploaded files work and are located in the media root under the profile\_pics folder.

models.py

```auto
class CustomUser(AbstractUser):
    # custom user account with username as primary identifier   
    email = models.EmailField(unique=True)
    username = models.CharField(max_length=150, unique=True)
    password = models.CharField(max_length=30)
  
    objects = CustomUserManager()
    
    REQUIRED_FIELDS = ['email']
    USERNAME_FIELD = 'username'
    
    class Meta:
        verbose_name = 'user'
        verbose_name_plural = 'users'
       

class Profile(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
    bio = models.TextField()
    profile_pic = models.ImageField(upload_to='profile_pics', default='default.jpg')
    first_name = models.CharField(max_length=50)
    middle_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    date_of_birth = models.DateField(null=True, blank=True)
    avatar = models.ImageField(upload_to='avatars', default='default.jpg')
    primary_language = models.CharField(max_length=50)
    secondary_language = models.CharField(max_length=50)
    primary_phone_number = models.CharField(max_length=15, validators=[phone_regex])
    pager_number = models.CharField(max_length=15)
    work_department = models.ForeignKey('Department', on_delete=models.CASCADE, null=True, blank=True) 
    is_provider = models.BooleanField(default=False)
    is_patient = models.BooleanField(default=False)
    is_pcp = models.BooleanField(default=False)
    # address = models.ForeignKey('Address', on_delete=models.CASCADE, null=True, blank=True)
    social_security_number = models.CharField(max_length=15)
    tax_identification_number = models.CharField(max_length=15)
    street_number = models.CharField(max_length=10)
    street_name= models.CharField(max_length=100)
    apt_number = models.CharField(max_length=10, blank=True, null=True)    
    city = models.CharField(max_length=50, blank=True)
    state = models.CharField(max_length=50, choices=US_STATES)
    country = models.CharField(max_length=50)
    postal_code = models.CharField(max_length=10)
    
    def __str__ (self):
        return self.user.username
    
    class Meta:
        verbose_name_plural = 'Profiles'
        verbose_name = 'Profile'

```

user\_navbar.html

```auto
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown"
              aria-expanded="false">
              <div>
                {% if not user.is_authenticated %}
                <img src="{% static 'media/anon_user.png' %}" class="rounded-circle" height="35" alt="" loading="lazy" />
                {% else %}
                <img src="{{ ***?????*** }}" class="rounded-circle" height="35" alt="" loading="lazy" />
                {% endif %}
               </div>
              {{ request.user.username }}
            </a>

```

---

<div class="post-metadata">

**Author:** ![HBarotov](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hbarotov/32/26901_2.png) [@HBarotov](https://forum.djangoproject.com/u/HBarotov)\
**Post date:** [September 24, 2024, 1:47am UTC](https://forum.djangoproject.com/t/how-to-get-profile-pic-in-navbar-from-logged-in-user-to-display/35044/2 "2024-09-24T01:47:59Z")

</div>

HI,

Assuming you have set MEDIA\_URL and MEDIA\_ROOT [correctly](https://djangocentral.com/managing-media-files-in-django/), you can access media files in your templates in this way:

```auto
{% if not user.is_authenticated %}
<img src="{% static 'media/anon_user.png' %}" class="rounded-circle" height="35" alt="" loading="lazy" />
{% else %}
<img src="{{ user.profile.profile_pic.url }}" class="rounded-circle" height="35" alt="{{ user.username }}" loading="lazy" />
{% endif %}

```

_Opinion part_

> Unless you define `related_name` in your `OneToOneField` , Django will use lowercased model name to access related object. So, `user.profile` should work.

I would recommend setting `related_name` to `profile` to be more explicit:

```auto
class Profile(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, related_name='profile')

```
