【发布时间】:2017-02-18 12:18:48
【问题描述】:
views.py
class ListPosts(generic.ListView):
template_name = 'posts/list_posts.html'
context_object_name = 'list_posts'
def get_queryset(self):
#kwargs={ "slug": user.slug }
qs = Post.objects.all().filter(user=self.request.user)
return qs
这里添加了过滤器,因此查询集只过滤登录用户的帖子。 我的问题是如何显示来自 URL 中的 slug 的用户价值的帖子
urls.py
url(r'^(?P<slug>[\w.@+-]+)/Posts/$', ListPosts.as_view(), name='user_posts'),
示例:username/posts
我知道这可以通过从 URL 传递 kwargs 来完成。但是如何在同一个视图中使用两个不同的模型呢?
【问题讨论】:
-
检查
request.GET()。