How to display user's photo that i follow in post's list in Django

169 views
Skip to first unread message

skerdi

unread,
Dec 22, 2016, 8:02:36 AM12/22/16
to Django users
In a  post list I've shown only the users posts that I follow but I'm not able to display their photo. 

Here is my Profile model, extending Django User model




































Here is my Post model:













































I want to show the photo of the user as shown below.
What would be the right queryset to display that.
Any help would be apriciated.







Melvyn Sopacua

unread,
Dec 22, 2016, 8:26:36 AM12/22/16
to django...@googlegroups.com
Hi,

not sure why you're screenshotting code instead of copying it...makes it
harder to point you at the mistake.

On Wednesday 21 December 2016 14:33:58 skerdi wrote:
> *In a post list I've shown only the users posts that I follow but I'm
> not able to display their photo. *

in Profile.get_photo() you set users to self.photo. I don't think you
wanted to do that.

--
Melvyn Sopacua

skerdi

unread,
Dec 22, 2016, 8:45:56 PM12/22/16
to Django users
That get_photo func, I'm not using it. It tried to do something and I forgot to delete it.
About screenshotting the code, I thought that it was more clarifying.

class ProfileManager(models.Manager):
use_for_related_fields = True

def all(self):
qs = self.get_queryset().all()
try:
if self.instance:
qs = qs.exclude(user=self.instance)
except:
pass
return qs

def toggle_follow(self, user, to_toggle_user):
user_profile = user.profile
if to_toggle_user in user_profile.following.all():
user_profile.following.remove(to_toggle_user)
adedd = False
else:
user_profile.following.add(to_toggle_user)
adedd = True
return adedd

def is_following(self, user, followed_by_user):
user_profile = user.profile
if followed_by_user in user_profile.following.all():
return True
return False


class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
following = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name='followed_by')
date_of_birth = models.DateField(blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
country = models.CharField(max_length=50, blank=True, null=True)
education = models.CharField(max_length=150, blank=True, null=True)
photo = models.ImageField(upload_to='profile photos/', blank=True, null=True)

objects = ProfileManager()

def __str__(self):
return 'Profile for user {}'.format(self.user.username)

def get_following(self):
users = self.following.all()
return users.exclude(username=self.user.username)

@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, *args, **kwargs):
if created:
Profile.objects.create(user=instance)
            instance.profile.save() 

class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL)
profile = models.ForeignKey(Profile)
title = models.CharField(max_length=200)
content = models.TextField()
image = models.FileField(upload_to='posts/', blank=True)
created = models.DateTimeField(auto_now_add=True, db_index=True)
modified = models.DateTimeField(auto_now=True)
slug = models.SlugField(unique=True)
likes = models.PositiveIntegerField(default=0)

@property
def total_likes(self):
return self.likes.count()

def __str__(self):
return self.title

class Meta:
ordering = ["-created"]

def get_absolute_url(self):
return reverse("detail", kwargs={"slug": self.slug})

def _get_unique_slug(self):
slug = slugify(self.title)
unique_slug = slug
num = 1
while Post.objects.filter(slug=unique_slug).exists():
unique_slug = '{}-{}'.format(slug, num)
num += 1
return unique_slug

def save(self, *args, **kwargs):
if not self.slug:
self.slug = self._get_unique_slug()
super(Post, self).save()

This is copy/pasting it :)
I still need to make that queryset to show a list of posts the users that I follow(and myself) with Profile combined or showing the post.content and profile.photo. 

Melvyn Sopacua

unread,
Dec 25, 2016, 7:02:50 PM12/25/16
to django...@googlegroups.com

On Thursday 22 December 2016 17:45:56 skerdi wrote:

> That get_photo func, I'm not using it. It tried to do something and I

> forgot to delete it.

 

Try this on the Profile model:

def get_photo_url(self, user, when_following=True):
if when_following:
try:
if self.following.get(user=user).count() > 0:
return user.profile.photo.url
return None
except
user.ObjectDoesNotExist:
return None
else
:
return user.profile.photo

--

Melvyn Sopacua

Andrew Beales

unread,
Dec 27, 2016, 11:41:22 AM12/27/16
to Django users
If I'm following correctly and you want to access author profile photos from querysets of Post objects: if you add related_name=‘profile’ to the user field in the Profile model then you can add a method to the Post model:

def author_profile_photo(self):
   
return self.author.profile.photo

and then access the photos in the template using {{ post.author_profile_photo }}
You will need to catch cases where there is no photo either in the class method or the template to avoid an error, eg.
{% if post.author_profile_photo %} {{ post.author_profile_photo }} {% else %} {{ <placeholder image>}} {% endif %}
Reply all
Reply to author
Forward
0 new messages