7

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Roger Liu
  • 1,768
  • 3
  • 16
  • 25

1 Answers1

10

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 }}
tbrlpld
  • 776
  • 6
  • 16
stalk
  • 11,934
  • 4
  • 36
  • 58
  • I know that, but I have customed my context in the get_context_data method, I add fields like 'can_show_distribute', I'm afraid if I use the for loop, I will also access the custom fields, but I just want to display the data returned by the get_queryset method. – Roger Liu May 09 '13 at 09:16
  • 2
    Well, you've done this: `context = super(GetStudentQueryHandler, self).get_context_data(**kwargs)`. So your context will include all fields, that `ListView` adds. To access `can_show_distribute` from template you shall do this: `{{can_show_distribute}}`, and not this: `{{studentinfo.can_show_distribute}}` – stalk May 09 '13 at 09:21
  • 1
    so you mean the "studentinfo" object just represent the queryset returned by the get_queryset method? ok I will have a try, if it works I will accept your answer, thank you – Roger Liu May 09 '13 at 09:23
  • @stalk Do you mean that for different views, the default context variable might differ? – hlkstuv_23900 Nov 07 '14 at 12:17
  • 2
    @tilaprimera by default, *all* instances of `ListView` have variable named [`object_list`](https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-display/#making-friendly-template-contexts). But, you can set specific to current view variable name in `context_object_name` view class property – stalk Nov 07 '14 at 12:33
  • Since the accessing of `can_show_distribute` also seemed to be in question, I have proposed to extend the accepted answer accordingly. – tbrlpld Mar 19 '19 at 20:57