How a loggedin user can view other user profile using django

I am working on a django project. When a user logs in, the user will be redirected to the profile page where he can view his username, email, profile picture, and list of blogpost created by him.

This is the user profile page - blog/researcher-profile.html

{% extends 'users/base.html' %}
{% block content %}
<div class="content-section">
    <div class="media">
      <img class="rounded-circle account-img" src="{{ user.profile_pic.url }}" width="125" height="125">
      <div class="media-body">
        <h2 class="account-heading" style="margin-left: 30px">{{ user.username }}</h2>
        <p class="text-secondary" style="margin-left: 30px">{{ user.email }}</p>
        <a href="{% url 'user-update' %}" class="btn btn-secondary btn-sm" style="margin-left: 30px;"> Edit Profile </a>
      </div>
    </div>
</div>
{% for post in posts %}
{% if post.approved %}
    <div class="card mb-3">
        <img class="card-img-top" src="{{ post.image.url }}" alt="Card image cap">
        <div class="card-body">
            <h5 class="card-title">{{ post.title|truncatechars:70 }}</h5>
            <p class="card-text">{{ post.content|truncatechars:200|safe }}</p>
            <a href="{% url 'post-detail' post.aid %}" class="btn btn-primary"> See Details </a>
        </div>
        <div class="card-footer text-secondary">
            <a class="mr-2" href="{% url 'researcher-profile' %}">{{ post.author }}</a>|| 
            {{ post.created|date:"F d, Y" }}
        </div> 
    </div>
{% endif %}
{% endfor %}
{% endblock content %}

urls.py

from django.contrib import admin
from django.urls import path
from users import views as user_views
from django.contrib.auth import views as auth_views
from blog.views import UserPostListView
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path("admin/", admin.site.urls),
    path('', user_views.home, name='home'),
    path('register/', user_views.register, name='register'),
    path('login/', auth_views.LoginView.as_view(template_name = 'users/login.html'), name='login'),
    path('logout/', user_views.logout_request, name='logout'),
    path('profile/', UserPostListView.as_view(), name='researcher-profile'), # user will be redirected to this page after logging in.
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

blog/view.py

from django.shortcuts import render
from .models import Post, Comment
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from .forms import PostForm, EditForm, CommentForm

# get all posts of the logged in user
class UserPostListView(LoginRequiredMixin, ListView):
    model = Post # query the post model to create the list of articles
    template_name = 'blog/researcher-profile.html'
    context_object_name = 'posts' # if I don't set context_object_name here, it will be object_list to iterate through the list of posts in the html page.
    ordering = ['-created']

    # return the list of items for this view
    def get_queryset(self):
        # return Post.objects.filter(author = self.request.user, approved=True) 
        return Post.objects.filter(author = self.request.user)

blog/models.py

from django.db import models
from django.utils import timezone
from django.contrib.auth import get_user_model
from django.urls import reverse
from ckeditor.fields import RichTextField # first I installed ckeditor by this command: pip install django-ckeditor

# Create your models here.
class Category(models.Model):
    cid = models.AutoField(primary_key=True, blank=True) 
    category_name = models.CharField(max_length=100)

    def __str__(self):
        return self.category_name


class Post(models.Model):
    aid = models.AutoField(primary_key=True)
    image = models.ImageField(default='blog-default.png', upload_to='images/')
    title = models.CharField(max_length=200)
    # content = models.TextField()
    content = RichTextField()
    created = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
    cid = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='specialization') 
    approved = models.BooleanField('Approved', default=False)

    def __str__(self):
        return self.title

   

users/models.py

from django.db import models
from blog.models import Category
from django.contrib.auth.models import AbstractUser

# Create your models here.
class CustomUser(AbstractUser):
    cid = models.ForeignKey(Category, on_delete=models.CASCADE)
    profile_pic = models.ImageField(default='default_person.jpg', upload_to='profile_pics')

Now, I want to view other user profile. How to do that? As I am using class based views, So can anyone provide solution of this problem please?

Please be more specific and provide more detail with what you want to accomplish here. This includes describing how you want whatever this is to work from a user’s perspective.

For example, are you wanting to let a user look at other people’s profiles? If so, then you need to decide how you want a user to select the other user to display.

Yes. I want a user to look at other people’s profiles. In researcher-profile.html, In the footer of a post, when the logged-in user clicks on the author name, he is redirected to this same profile page which is fine if he is the author of the post.

But when a logged-in user goes to post number 3 which is the post_detail page, for example, localhost:8000/post/3/ and is not the author of this post and clicks on the username in the footer, then he is redirected to his own profile page instead of going to the author profile’s page.

Here is the post_detail.html

{% extends 'users/base.html' %}
{% block content %}
    <div class="card mb-3">
        <img class="card-img-top" src="{{ object.image.url }}" alt="Card image cap">
        <div class="card-body">
            <h5 class="card-title">{{ object.title }}</h5>
            <p class="card-text">{{ object.content|safe }}</p>
            <p class="card-text"> <b> Specialization: </b> {{ object.cid }} </p>
            {% if object.author == user %}
            <div>
                <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'post-update' object.aid %}">Edit</a>
                <a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'post-delete' object.aid %}">Delete</a>
            </div>
            {% endif %}
        </div>
        <div class="card-footer text-secondary">
            <a class="mr-2" href="{% url 'researcher-profile' %}">{{ object.author }}</a>|| 
            {{ object.created|date:"F d, Y" }}
        </div> 
    </div>
{% endblock content %}

views.py

class UserPostDetailView(LoginRequiredMixin, DetailView):
    model = Post

url.py

 path('post/<int:pk>/', UserPostDetailView.as_view(), name='post-detail'),

How to solve this problem?

I don’t see in any of the template fragments posted here where you’re creating a link to 'post-detail'. Where are you using it?