Django 如何使用基于类的视图渲染多个对象
我尝试使用基于类的视图渲染多个对象,但出现了一个错误 这是我的密码:Django 如何使用基于类的视图渲染多个对象,django,class-based-views,Django,Class Based Views,我尝试使用基于类的视图渲染多个对象,但出现了一个错误 这是我的密码: class AssociatedList(WizardRequiredMixin, TemplateView): template_name = "profile/associated_accounts.html" def get_context_data(self, **kwargs): context = super(AssociatedList, self).get_context_da
class AssociatedList(WizardRequiredMixin, TemplateView):
template_name = "profile/associated_accounts.html"
def get_context_data(self, **kwargs):
context = super(AssociatedList, self).get_context_data(**context)
all_envelopes = Envelope.objects.filter(
user=request.user).exclude_unallocate()
free_limit = account_limit(request, 15, all_envelopes)
facebook = FacebookProfile.user_profiles(request.user)
google = GoogleProfile.user_profiles(request.user)
twitter = TwitterProfile.user_profiles(request.user)
context.update = ({
'facebook': facebook,
'google': google,
'twitter': twitter,
'free_limit': free_limit,
})
return context
错误:
local variable 'context' referenced before assignment
我总是通过在函数开头调用
super
然后附加上下文来覆盖get\u context\u data
def get_context_data(self, *args, **kwargs):
context = super(AssociatedList, self).get_context_data(*args, **kwargs)
all_envelopes = Envelope.objects.filter(
user=self.request.user).exclude_unallocate()
free_limit = account_limit(self.request, 15, all_envelopes),
facebook = FacebookProfile.user_profiles(self.request.user),
google = GoogleProfile.user_profiles(self.request.user),
twitter = TwitterProfile.user_profiles(self.request.user),
context.update({
'facebook': facebook,
'google': google,
'twitter': twitter,
'free_limit': free_limit,
})
return context
这是文档中使用的模式
更新
您刚才添加的错误表明您的类有错误。听起来您需要定义queryset
属性或model
属性
继承的
ListView
类要求您定义视图返回的模型(即YourModel.objects.all()
)。或者要返回的特定查询集(例如YourModel.objects.filter(您的\u字段=某个\u变量)
)。我总是通过在函数开头调用super
然后附加上下文来覆盖get\u context\u data
def get_context_data(self, *args, **kwargs):
context = super(AssociatedList, self).get_context_data(*args, **kwargs)
all_envelopes = Envelope.objects.filter(
user=self.request.user).exclude_unallocate()
free_limit = account_limit(self.request, 15, all_envelopes),
facebook = FacebookProfile.user_profiles(self.request.user),
google = GoogleProfile.user_profiles(self.request.user),
twitter = TwitterProfile.user_profiles(self.request.user),
context.update({
'facebook': facebook,
'google': google,
'twitter': twitter,
'free_limit': free_limit,
})
return context
这是文档中使用的模式
更新
您刚才添加的错误表明您的类有错误。听起来您需要定义queryset
属性或model
属性
继承的
ListView
类要求您定义视图返回的模型(即YourModel.objects.all()
)。或者返回特定的查询集(例如YourModel.objects.filter(您的\u字段=某个\u变量)
)。因为这是一个列表视图,您需要告诉它您将使用模型或queryset
列出什么。在这种情况下,您不想使用ListView,因为您正在覆盖获取上下文数据,所以您可能应该使用TemplateView或类似的东西。因为这是一个ListView,您需要告诉它您将使用模型或查询集
列出什么。在这种情况下,您不想使用ListView,因为您正在覆盖获取上下文数据,所以您可能应该使用TemplateView或类似的工具。尝试以下方法:
class AssociatedList(WizardRequiredMixin, ListView):
template_name = "profile/associated_accounts.html"
model = Envelope
def get_queryset(self):
return Envelope.objects.filter(user=self.request.user).exclude_unallocate()
def get_context_data(self, **kwargs):
context = super(AssociatedList, self).get_context_data(**kwargs)
context.update({
'facebook': FacebookProfile.user_profiles(self.request.user),
'google': GoogleProfile.user_profiles(self.request.user),
'twitter': TwitterProfile.user_profiles(self.request.user),
'free_limit': account_limit(self.request, 15, context['envelope_list']),
})
return context
您不需要模型具有queryset,但定义它是一种很好的实践。
在模板中,使用对象列表或信封列表,而不是所有的信封,这样您就可以开始了
注:关于CBV的良好知识来源。尝试以下方法:
class AssociatedList(WizardRequiredMixin, ListView):
template_name = "profile/associated_accounts.html"
model = Envelope
def get_queryset(self):
return Envelope.objects.filter(user=self.request.user).exclude_unallocate()
def get_context_data(self, **kwargs):
context = super(AssociatedList, self).get_context_data(**kwargs)
context.update({
'facebook': FacebookProfile.user_profiles(self.request.user),
'google': GoogleProfile.user_profiles(self.request.user),
'twitter': TwitterProfile.user_profiles(self.request.user),
'free_limit': account_limit(self.request, 15, context['envelope_list']),
})
return context
您不需要模型具有queryset,但定义它是一种很好的实践。
在模板中,使用对象列表或信封列表,而不是所有的信封,这样您就可以开始了
另外,关于CBV的知识来源很好。我也有同样的想法@catherine这样做解决了问题吗?好的,这是添加模型后的新错误,在赋值之前引用了局部变量“context”。渲染一个模型真的很容易。但是如何在一个基于类的视图中呈现多个模型?只需从TemplateView继承而不是ListView,并使用我的答案中的模式添加上下文。导致您最近错误的问题是由于一个小的复制粘贴错误:context=super(AssociatedList,self)。获取上下文数据(**kwargs)应该使用而不是context=super(AssociatedList,self)。获取上下文数据(**context)
我也这么想@catherine这样做解决了问题吗?好的,这是添加模型后的新错误,在赋值之前引用了局部变量“context”。渲染一个模型真的很容易。但是如何在一个基于类的视图中呈现多个模型?只需从TemplateView继承而不是ListView,并使用我的答案中的模式添加上下文。导致您最近错误的问题是由于一个小的复制粘贴错误:context=super(AssociatedList,self)。获取上下文数据(**kwargs)应使用
而不是context=super(AssociatedList,self)。获取上下文数据(**context)
@Ngenator我发布错误正如Ngenator在我回答的评论中指出的,您在定义“context”的语句中包含了对“context”的引用。这是我的复制粘贴错误。我已经更新了我的答案,只需将函数第一行中的“**context”替换为“**args,**kwargs”。@Ngenator我发布了错误,正如Ngenator在对我的答案的评论中指出的,您在定义“context”的语句中包含了对“context”的引用。这是我的复制粘贴错误。我已经更新了我的答案,只需将函数第一行中的“**上下文”替换为“*args,**kwargs”。我真的不知道基于类的视图。我用来呈现多个查询集的正确视图是什么?在这种情况下,因为您正在重写get\u context\u data
方法,所以TemplateView可以工作。我真的不知道基于类的视图。我用于呈现多个查询集的正确视图是什么?在这种情况下,由于您正在重写get\u context\u data
方法,TemplateView将起作用。您的查询集也有request.user,您将如何调用self?它再次给我一个错误。我的免费限额呼叫所有信封哪个查询是您在“获取”查询中放入的信封我也希望您的答案是最好的,但我无法检查两个答案:/your queryset have all request.user,您如何呼叫self?这又给了我一个错误。我的免费上限呼叫所有信封哪个查询是您在“获取”查询中放入的信封我也希望您的答案是最好的,但我无法检查两个答案:/