문제

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?

도움이 되었습니까?

해결책

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 }}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top