My code is like this: I custom my context and want to access my query set in template
class GetStudentQueryHandler(ListView): template_name = 'client.html' paginate_by = STUDENT_PER_PAGE context_object_name = 'studentinfo' def get_context_data(self, **kwargs): context = super(GetStudentQueryHandler, self).get_context_data(**kwargs) context['can_show_distribute'] = self.request.user.has_perm('can_show_distribute_page') context['form'] = QueryStudentForm return context def get_queryset(self): The question is : how to access the queryset returned by the get_queryset method in templates? I know I can access the custom attributes like studentinfo.can_show_distribute, how to access the query data?
1 Answer
As it written here, the default context variable for ListView is objects_list
So in template it can be accessed as following:
{% for obj in objects_list%} {{obj.some_field}} {% endfor %} Also, it can be set manually with context_object_name parameter (as in your example):
class GetStudentQueryHandler(ListView): # ... context_object_name = 'studentinfo' # ... and in template:
{% for obj in studentinfo %} {{obj.some_field}} {% endfor %} To access the additonally added field can_show_distribute from the context in the template:
{{ can_show_distribute }} 6