I've just started using class-based views in django. But there's an issue that is confusing for me. I ran the following code snippet with django 1.4.1 with multithreaded development server.
class TestView(TemplateView):
template_name = 'test.html'
count = 0
mylist = [1, ]
def get(self, request, *args, **kwargs):
self.count += 1
self.mylist.append(self.mylist[-1] +1)
context = self.get_context_data(**kwargs)
return self.render_to_response(context)
def get_context_data(self, **kwargs):
context = super(TestView, self).get_context_data(**kwargs)
context['count'] = self.count
context['mylist'] = self.mylist
return context
The template just outputs the context variables count and mylist. When this view is called e.g. up to 5 times the output will look like this:
count: 1
mylist: [1, 2, 3, 4, 5, ]
And now I'm confused. The django docs says, that each request has its own individual class instance.
So how it is possible to extend mylist over several requests? Why the count variable was not incremented?