无法在社交网络项目中获取 post 的详细视图

Can't get the detail view of a post in a social network project

我正在用 django 构建一个简单的社交网络。

在我的社交 "home" 中,我有所有用户发布的所有 post 的列表,以及作者和发布日期。发布日期有 link 到 url 应该 return 用户发布的特定 post 的详细视图。

然而,当我点击它时,它会显示其作者发布的所有 post 的列表(这在我点击 [=58] 的作者 link 时也有效=]).

所以两者

www.mysocial.com/posts/by/ciccio/7/

www.mysocial.com/posts/by/ciccio/

将我转到同一页面,即 ciccio 的 post 列表。

我将向 urls.py、views.py 和 models.py 展示它们都包含在我的 "posts" 应用程序中 (myproject > posts)

这是我的 urls.py

# shows all user posts
url(r'by/(?P<username>[-\w]+)', 
views.UserPosts.as_view(), 
name='for_user'),

# shows specific post
url(r'by/(?P<username>[-\w]+)/(?P<pk>\d+)/$', 
views.PostDetail.as_view(), 
name='single'),

我的views.py

class PostList(SelectRelatedMixin, generic.ListView):
    model = Post
    select_related = ('user', 'group')

class UserPosts(generic.ListView):
    model = models.Post
    template_name = 'posts/user_post_list.html'

    def get_queryset(self):
        try:
            #prende i post fatti dal'username che è lo stesso di quello che è loggato per guardarli
            self.post_user = User.objects.prefetch_related('posts').get(username__iexact=self.kwargs.get('username'))

        except User.DoesNotExist:
            raise Http404

        else:
            return self.post_user.posts.all()

    def get_context_data(self,**kwargs):
        context = super().get_context_data(**kwargs)
        context['post_user'] = self.post_user
        return context

class PostDetail(SelectRelatedMixin, generic.DetailView):
    model = models.Post
    select_related = ('user', 'group') #group senza apostrofo?

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(
            user__username__iexact=self.kwargs.get('username'),
            # maybe smth is needed here?
            )

我的models.py

User = get_user_model()


# Create your models here.

class Post(models.Model):
    user = models.ForeignKey(User, related_name="posts", on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now=True)
    message = models.TextField()
    message_html = models.TextField(editable=False)


    group = models.ForeignKey(Group, related_name="posts", null=True, blank=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.message

    def save(self, *args, **kwargs):
        self.message_html = m.html(self.message)
        # in questo modo quando si fa un markdon si mette un link nel loro post
        super().save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('posts:single', kwargs={'username':self.user, 'pk':self.pk})

    class Meta:
            ordering = ['-created_at']
            unique_together = ['user', 'message'] 

我的模板显示 post 的作者和发布日期:

    <span class="username">
        <!--  mi porta a tutti i post fatti da quell'user -->
        <a href="{% url 'posts:for_user' username=post.user.username %}">@{{ post.user.username }}</a>
    </span>

    <a> – </a>

    <!-- il time tag posta il tempo -->
    <time class="time">
    <a href="{% url 'posts:single' username=post.user.username pk=post.pk %}">{{ post.created_at }}</a>

    </time>

模型"User"是django默认的models.User

问题一定出在 PostDetail 视图中,因为我通过单击 url 验证了我的模板显示 post 正确的 pk (post.pk)

可能视图中缺少过滤器?看我的 # 评论

第一个 url 末尾缺少一个 /$:

url(r'by/(?P<username>[-\w]+)/$', 
    views.UserPosts.as_view(), 
    name='for_user'),

但我仍然无法理解 url 用于显示显示所有用户的 post 的视图如何影响 url 的行为,该行为应该只显示特定的post.