Reputation: 370
I hope you're well. I'm beginning with Django.
I'd like to create - like facebook - a public profile. I've already created a UserProfileUpdateView with country, adresse, image, ...
When a user post something I'd like to have a link to his public profile (country, adresse, image, ... + posts):
class UserPostView(ListView):
template_name = 'user_post.html'
model = Post
context_object_name = 'posts'
def get_context_data(self, **kwargs):
context = super(UserProfileView, self).get_context_data(**kwargs)
context['userprofile'] = UserProfile.objects.get(user=self.request.user)
return context
def get_queryset(self):
return Post.objects.filter(user=self.kwargs['pk'])
A - I'd like to display the public profile link with username (which is unique) and not with a number. Does anyone has an idea about how I can solve this?
path('<int:pk>/',UserPostView.as_view(),name="user_posts"),
UserProfile (user app)
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
street = models.CharField(null=True,blank=True,max_length=300)
number_street = models.CharField(null=True,blank=True,max_length=20)
street_detail = models.CharField(null=True,blank=True,max_length=300)
town = models.CharField(null=True,blank=True,max_length=60)
zipcode = models.CharField(null=True,blank=True,max_length=20)
country = models.CharField(null=True,blank=True,max_length=60)
image = models.ImageField(null=True,blank=True,default='user/user-128.png', upload_to='user/')
slug = models.SlugField(editable=False)
def save(self, *args,**kwargs):
self.slug = slugify(self.user.username)
super(UserProfile, self).save(*args, **kwargs)
img = Image.open(self.image.path)
if img.height > 200 or img.width > 200:
new_size = (200, 200)
img.thumbnail(new_size)
img.save(self.image.path)
def __str__(self):
return self.user.username
Post (nutriscore app)
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
url_image = models.URLField(max_length=200, default=None)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
updated_on = models.DateTimeField(auto_now= True)
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = unique_slugify(self, slugify(self.title))
super().save(*args, **kwargs)
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
Upvotes: 0
Views: 134
Reputation: 2084
If the user in the Post
model is a foreign key and is from django.contrib.auth.models.User
, then first scan the User table from username
and then scan the UserProfile table using the id as follows
user = User.objects.filter(username=name).values()
user_id = user[0]['id'] # You get the id of the user
userprofile = UserProfile.objects.filter(user_id=userprofile).values() # Scan the UserProfile table using the id obtained above
user_post = Post.objects.filter(user = user_id) # post by authenticated user
Upvotes: 1