Show user image on navigation bar

I've been working on creating my own blog website.

Initially, I was able to allow users to upload their profile picture and have it displayed next to their posts. Now, I want that same image to appear in the navigation bar as a profile image. However, when I try to implement it using the same method as with the posts, it doesn't work and ends up looking like this:

https://i.sstatic.net/1wTyE.png

For the posts, I created a profile image model and accessed the variable within the HTML file. But unfortunately, the same approach does not seem to work for displaying it in the navigation bar.

I am using Python and Django for the backend of my website.

Below is the model code for the profile image:

from django.db import models
from django.contrib.auth.models import User
from PIL import Image

class Profile(models.Model):
  user = models.OneToOneField(User, on_delete=models.CASCADE)
  image = models.ImageField(default="default.jpg", upload_to="profile_pics")

  def __str__(self):
     return f"{self.user.username} Profile"

  def save(self, *args, **kwargs):
     super().save(*args, **kwargs)

      img = Image.open(self.image.path)
      if img.height > 300 or img.width > 300:
          output_size = (300, 300)
          img.thumbnail(output_size)
          img.save(self.image.path)

This is how I reference it in my HTML file for displaying it next to posts:

<img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}" id="img"> 

When I use any other image (whether from a URL or a local file), it works perfectly. But when I try to call the image using that variable, it doesn't work.

It's worth noting that the navigation bar and post sections are in different HTML files. It seems like the HTML file for the navigation bar doesn't have access to that variable?

What could be causing this issue? Any guidance would be greatly appreciated.

Answer №1

Give this a shot:

<img class="circular-image user-img" src="{{ request.user.profile.image.url }}" id="profile-pic"> 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Styling HTML Text Using Your Own Unique CSS Class Style

After developing some CSS classes to define specific hex colors for my web application, I ran into issues applying these styles to text in the HTML. Despite my best efforts, I am struggling to make the styles show up as intended. CSS .custom-orange-color ...

show numerical values as images in sql

Currently, I'm in the process of developing a website for a neighbor who is considering opening a new restaurant. One of the features I need to work on is a page dedicated to testimonials and reviews from customers. My goal is to use SQL to store the ...

Instructions on adding a solid border color to a circular div using CSS

Hey there! I'm curious, is it possible to style a straight border color on a rounded div using CSS? Here I have some basic Tailwind CSS code. <div class="rounded-2xl w-72 h-20 border-l-4 border-l-yellow-500"> ... </div> In the ...

Converting to HTML character entities and converting back again

Whenever I replace text in my chat room, it shows up as HTML Character Entities in the box. However, I want it to display the actual characters typed in when shown in the chat room. To achieve this, I am using the following code to prevent any HTML from be ...

Is Intellisense within HTML not available in SvelteKit when using TypeScript?

Having trouble with intellisense inside HTML for a simple page component. Also, renaming properties breaks the code instead of updating references. Typescript version: 4.8.4 Below is the code snippet: <script lang="ts"> import type { Bl ...

Executing Python output in HTML with Django

I am currently working on a Django project and in the views.py file of my app, I have some outputs stored in a dictionary. The code snippet from views.py is as follows: from django.shortcuts import render def allblogs(request): a = request.GET[' ...

Gunicorn Encounters Internal Server Glitches

Running a Django application on a Gunicorn server can pose some challenges, especially when the server tends to crash frequently. This can lead to all Gunicorn workers shutting down at once, without generating any logs in Django or django-sentry. Despite t ...

Analyzing data using Python regex and saving it in a list

Hey there! I've got a query related to utilizing regex in python. Imagine having a string code comprising multiple lines of formulas with variables or numeric values. For instance: code1 = ''' g = -9 h = i + j year = 2000 month = 0xA d ...

Design of the content layout for the PHP mail function

After submitting a form on a website, the text entered is saved in a database. I attempted to send this text via email to a recipient using the following code: $recipient = $emailaddress; $subject = 'xxxx'; $header = 'From: xxxx < ...

Using a glass of water as a metaphor to illustrate advancements in CSS and JQuery

I've been working on a new app and have recently started implementing some significant changes. The app has a unique feature that allows users to track their water intake and compare it to their set goal. One key aspect I am struggling with is creati ...

Trouble with Clicking to Show Content

<div id="uniqueDiv"> <button id="uniqueButton1" class="uniqueClass">Hit</button> <button id="uniqueButton2" class="uniqueClass">Run</button> </div> <div id="uniqueDiv2"> <p id="uniqueId1"></p>< ...

Is there a method in django-taggit that allows for retrieving all tags linked to a specific model?

While I know how to retrieve all tags, my challenge lies in extracting only the tags connected to a specific model. Is there a way to accomplish this without retrieving all possible tags? I'm aware that I could obtain the model first and then extrac ...

Adjust the text placement on an adaptable image according to a particular section of the image

For accessibility reasons, I have an image that requires text to be overlaid on it. The image is responsive thanks to Bootstrap's img-responsive class. I need the text to stay positioned correctly on the image even as its size changes. Is there a way ...

bulk upload with the ability to remove files

I'm in the process of creating a multiple file upload with delete option, similar to the design shown in this image: https://i.sstatic.net/RomOR.jpg I have been following an example on this JS Fiddle demo. My goal is to eliminate the button with the ...

I've found that my div element refuses to be centered on the screen unless I explicitly define

Trying to center a div on the page using jQuery has proven to be a bit tricky. It seems that without specifying a height for the div in the CSS, the centering doesn't work as expected. The challenge lies in the fact that the div is meant to resize bas ...

What is the best way to implement autoplay sound on a JavaScript webpage?

I'm currently working on a web system aimed at monitoring values from a database, and I have the requirement to trigger a sound alert when a specific range of values is received. Despite trying various examples found online, none of them have been suc ...

Button click functionality for updating in Classic ASP is functioning correctly exclusively in Internet Explorer and not in other web browsers

Currently, I am in the process of enhancing a Classic ASP application. Within this application, there is an HTML table that is populated from a SQL database. One cell within the table contains checkboxes. Upon clicking one of these checkboxes, a popup wind ...

Hover Effect: Show Listbox When Textbox is Hovered Using CSS

I'm having trouble showing a listbox when hovering over a textbox. Here's the code: <table id="Search"> <tr> <td> <asp:TextBox runat="server" ID="topics" CssClass="TT"></asp:TextBox> & ...

Comparison of Ajax and submit: Why am I not receiving identical responses?

Utilizing Ajax for submission and all global variables ending in box. Note that the initialization code is functioning correctly. function begin() { if (confirm("Proceed at your own risk as all consequences are yours!!")) { var usernameNa ...

Attempting to simulate a camera shutter effect using div elements

I've been attempting to create a circular camera shutter, but I'm facing difficulties in making it look accurate. This is the desired outcome: https://i.sstatic.net/oS7gT.jpg The first 'petal' should be positioned lower than the last ...