Context
Context is a frequently used word when you write views. It is a set of dictionary-type data mapping variable names to variable values that are transferred from views to templates.
Check context data in your terminal
To understand the concept of context, let's print the context data for the list page.
Add the yellow part in the CourseList view code, which redirects the output (the context data for the list page) to your terminal.
class CourseList(ListView):
# model = LearningCourse
# queryset = LearningCourse.objects.order_by('-title')
queryset = LearningCourse.objects.filter(title__contains='Docker')
:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
print(context)
You don't see any list items when you visit the list page in your browser. Instead, you'll get output in your terminal like shown below. (In the actual case, there are no line breaks. We added line breaks to make the code easy to read.)
[04/Apr/2023 01:43:32] "GET /employee-learning/course-list/ HTTP/1.1" 200 1748
{'paginator': None,
'page_obj': None,
'is_paginated': False,
'object_list': <QuerySet [<LearningCourse: Docker Introduction>, <LearningCourse: Docker Advanced & Kubernetes>]>,
'course_object_list': <QuerySet [<LearningCourse: Docker Introduction>, <LearningCourse: Docker Advanced & Kubernetes>]>,
'view': <employee_learning.views.CourseList object at 0x1106026d0>}
You can see that the context carries several data sets that are used to display list items on the list page.