Answer the question
In order to leave comments, you need to log in
How to display profile entries of each Django user?
Hello. Faced a problem. I made each user their own account, gave the opportunity to add their own entries. On the personal page, I was able to display the records of the authorized user. But I don’t understand how, when going to the personal pages of other users, display their records (I note that personal data (recorded in a different model) is displayed for each user).
Model
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=30, verbose_name='Имя', default='Новый')
last_name = models.CharField(max_length=30, verbose_name='Фамилия', default='Пользователь')
position = models.CharField(max_length=100, verbose_name='Должность', default='Должность сотрудника')
slug = models.SlugField(verbose_name="URL адрес", unique=True, blank=True, null=True, default='{}')
bio = models.TextField(max_length=500, blank=True, verbose_name='Информация о человеке')
birth_date = models.DateField(null=True, blank=True, verbose_name='Дата рождения')
image = models.ImageField(null=True, blank=True, upload_to="image_profil", verbose_name='Фото профиля', default='default_photo')
#Эти данные получается отображать по ссылке в профиль
def get_absolute_url(self):
return reverse('user_url', kwargs={'slug': self.slug})
class Post(models.Model):
datetime = models.DateTimeField(verbose_name=u"Дата", auto_now_add=True)
author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=u"Автор", related_name="posts")
text = models.CharField(max_length=1000, verbose_name=u"Текст", null=True, blank=True)
image = models.FileField(verbose_name=u"Картинка", null=True, blank=True, default='default_photo')
def user(request, slug):
#Получение данных профиля конкретного пользователя, отображает данные пользователя
get_user = get_object_or_404(Profile, slug__iexact=slug)
#Новости на личной странице пользователя
#А вот здесь уже не выходит задаю id=7 и на любой пользовательской странице
#выдает записи пользователя с id=7 это как бы логично, но мне нужен другой результат
user_post = User.objects.get(id=7)
user_context=Post.objects.filter(author=user_post)
context={
'user_data': get_user,
'user_context':user_context
}
return render(request, 'accounts/profile_user.html',context)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question