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?