Admin profile view after login instead of displaying user profile in django

I am working on a django project.There are two types of users. One is admin and another one is researcher. So I want to display admin profile when admin logs in using the login page instead of researcher profile. I know django has an admin panel. But I want to build custom admin profile. So when admin logs in using the login page created by myself, admin can see the posts created by users. Also he can add category, approve post, delete post. On the other hand, when researchers logs in, users can see their profile page.

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

# Create your models here.
class Category(models.Model):
    cid = models.AutoField(primary_key=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 = 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)
    like = models.ManyToManyField(get_user_model(), related_name='likes', blank=True)

    def __str__(self):
        return self.title

users/model.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, verbose_name='specialization', blank=True, null=True)
    profile_pic = models.ImageField(default='default_person.jpg', upload_to='profile_pics')

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 'other-people-profile' post.author.username %}">{{ post.author }}</a>|| 
            {{ post.created|date:"F d, Y" }}
        </div> 
    </div>
{% endif %}
{% endfor %}
{% endblock content %}

blog/views.py -> so this is the view for the researcher.When researcher will login, the user can see email, profile pic, username, and list of posts created by themselves if the admin approves posts.

from django.shortcuts import render, get_object_or_404
from .models import Post, Category
from django.views.generic import ListView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import get_user_model

User = get_user_model()

# get all posts of the loggedin user
class UserPostListView(LoginRequiredMixin, ListView):
    model = Post 
    template_name = 'blog/researcher-profile.html'
    context_object_name = 'posts' 
    ordering = ['-created']

    # return the list of items for this view only from a certain user
    def get_queryset(self):
        # return Post.objects.filter(author = self.request.user, approved=True) 
        return Post.objects.filter(author = self.request.user)
    
    def get_context_data(self, *args, **kwargs):
        cat_menu = Category.objects.all()
        context = super(UserPostListView, self).get_context_data(*args, **kwargs)
        context['cat_menu'] = cat_menu
        return context

urls.py

path('profile/', UserPostListView.as_view(), name='researcher-profile'),

What do I have to do? Do I need another admin-profile.html and URL for admin profile view. And what will be the logic in views.py so that I can see the admin profile. Note that, I used class based views for the researcher-profile view in the views.py mentioned above.

It is an entire topic. I recommend you use Django Permissions and Role-Based Access. A simple google search will yield productive tutorials for that.

Back to Top