Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在profile[django]中显示来自其他视图的所有用户文章_Django_Django Views_Django Templates - Fatal编程技术网

在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视图中的详细信息

谢谢!我去查一下。有可能在上下文中创建多个模型?您可以添加任何您真正想要的。它只是返回一个字典,然后您的模板可以访问它