I'm building a blog using Django, I'm very very new using this tool and I've been trying to figure out how to include the user's profile picture in the users posts view.
Basically in the users posts view, I made a filter ---> www.site.com/user/"username"
, so you can see all the user posts by date posted. All is working, but I'm not getting the user profile picture.
I really don't know if this is a problem of the query_set or if I'm missing something.
views.py
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.contrib.auth.models import User
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Post
class UserPostListView(ListView):
model = Post
template_name = 'blog/user_posts.html'
context_object_name='posts'
paginate_by = 15
def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get('username'))
return Post.objects.filter(author=user).order_by('-date_posted')
user_posts.html I'm getting the username but not the user profile.
<div class="col-auto"><img class="img-profile-st" src="{{ post.author.profile.image.url }}"></div>
<h1 class="mb-3" style="text-align: center;">Posted by {{ view.kwargs.username }} ({{ page_obj.paginator.count }})</h1>
models.py
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')