在profile[django]中显示来自其他视图的所有用户文章
我在新闻应用程序中有文章模型在profile[django]中显示来自其他视图的所有用户文章,django,django-views,django-templates,Django,Django Views,Django Templates,我在新闻应用程序中有文章模型 class Article(Created, HitCountMixin): title = models.CharField(max_length=120) author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) snippet = models.TextField(null=False) 鉴于: class AllArticlesL
class Article(Created, HitCountMixin):
title = models.CharField(max_length=120)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
snippet = models.TextField(null=False)
鉴于:
class AllArticlesListView(ListView):
template_name = 'news/articles_list.html'
model = Article
paginate_by = 5
def get_queryset(self):
return self.model.objects.all().order_by('-pk')
在另一个应用程序(用户应用程序)中,我有配置文件模型:
class Profile(AbstractUser):
bio = models.TextField(blank=True, null=True, default='Brak')
鉴于
class ProfileView(HitCountDetailView):
model = Profile
template_name = 'profile/profile.html'
在项目url中我有:
path("<slug:slug>", ProfileView.as_view(), name='profile'),
path('all/', AllArticlesListView.as_view(), name='all_articles_list'),
如何显示其用户配置文件中的所有用户文章?
class ProfileView(DetailView):
model = Profile
template_name = 'profile/profile.html'
count_hit = True
将你的文章添加到上下文中
def获取上下文数据(self,**kwargs):
“”“将项目添加到上下文”“”
context=super()。获取上下文数据(**kwargs)
如果self.request.user.u经过身份验证:
上下文['articles']=Article.objects.filter(author=self.request.user)
返回上下文
您可以在这里找到
DetailView
和其他django视图中的详细信息 谢谢!我去查一下。有可能在上下文中创建多个模型?您可以添加任何您真正想要的。它只是返回一个字典,然后您的模板可以访问它