Я пытаюсь получить отдельные сообщения определенного пользователя. Если я нажимаю на определенного пользователя, я хочу получить все сообщения, которые он опубликовал Django

Я вообще-то новичок в программировании, поэтому я просто получаю пустую страницу, без ошибок

имя_приложения= исход`

this is my model
    class Profile(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile', 
        blank=True, null=True)
        bio = models.TextField(max_length=500, null=True)
        location = models.CharField(max_length=30, null=True)

        def __str__(self):
           return self.user.username

        def get_absolute_url(self):
           return reverse('outcome:userprofile-detail', kwargs=
         {'pk': self.pk
     })

    class Post(models.Model):
        text = models.TextField(max_length=255)
        profile = models.ForeignKey('Profile', null=True, on_delete = models.CASCADE, 
            related_name='create')
        overview = models.CharField(max_length=255)

        def __str__(self):
            return self.text 
the view
    class Userlist(LoginRequiredMixin, ListView):
        model = Profile
        template_name = 'outcome/user-list.html'

    
    class UserDetail(LoginRequiredMixin, DetailView):
        model = Profile
        template_name = 'outcome/userprofile_detail.html'
**user_detail template**
    {% for i in post.profile_set.all %}
      **`trying to loop over the post`**

        {{i.text}}
    {% endfor %}

я пробовал это уже некоторое время и не знаю, от чего это зависит - от шаблона или от представления.

Вы можете переопределить метод get_context_data следующим образом :

class UserDetail(LoginRequiredMixin, DetailView):
    
    model = Profile
    template_name = 'outcome/userprofile_detail.html'
    
    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        # Add in a QuerySet of the user posts
        all_posts = Post.objects.all()
        user_posts = all_posts.filter(profile_id=self.request.user.profile.id)
        context['user_posts'] = user_posts
        return context

user_detail.html

{% for post in user_posts %}
    {{post.text}}
    {{post.overview}}
{% endfor %}
Вернуться на верх