Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to set instance variables in a Django class based view?

I trying out Django's class based views (CBVs).

class BlahView(TemplateView):     template_name = 'blah/blah.html'     def get_context_data(self, **kwargs):         #code...      def get(self, request, **kwargs):         #more code... 

Now, I know that I can get the request params from self.request. Now say I want to parse these request params and store them within the class. Can I store those in self.xxx? Now, obviously based on how classes work, this seems straightforward.

But I can't make out the flow of control, looking at the definition of View (superclass of TemplateView). The source mentions as_view() to be the 'entry-point'

I thought of setting my instance variables at the beginning of get_context_data() but that doesn't seem right to do initialization there.

Can I define an __init__() for my CBV? If so, will there be threading issues or something where multiple page-accesses possibly work with a global instance of my parsed data?

I know this sounds a bit messy, but I'm just a bit confused with the code flow in CBVs.

like image 472
ForeverLearnNeverMaster Avatar asked Jul 12 '12 09:07

ForeverLearnNeverMaster


People also ask

Should I use class based views in Django?

Generic class-based views are a great choice to perform all these tasks. It speeds up the development process. Django provides a set of views, mixins, and generic class-based views. Taking the advantage of it you can solve the most common tasks in web development.

Can classes have instance variables?

Instance variables can be used by all methods of a class unless the method is declared as static.

Which is better class based view or function based view Django?

Class based views are excellent if you want to implement a fully functional CRUD operations in your Django application, and the same will take little time & effort to implement using function based views.

Are instance variables set within a controller method accessible within a view?

Are instance variables set within a controller method accessible within a view? Yes, any instance variables that are set in an action method on a controller can be accessed and displayed in a view.


1 Answers

According to the source of django.views.generic.base.View.as_view:

  • on django startup, as_view() returns a function view, which is not called
  • on request, view() is called, it instantiates the class and calls dispatch()
  • the class instance is thread safe

According to the source of django.views.generic.base.View.__init__, the request object is out of scope at this point so you can't parse it in your own constructor overload.

However, you could parse the request and set class view instance attributes in an overload of django.views.generic.base.View.dispatch, this is safe according to the source:

class YourView(SomeView):     def dispatch(self, request, *args, **kwargs):         # parse the request here ie.         self.foo = request.GET.get('foo', False)          # call the view         return super(YourView, self).dispatch(request, *args, **kwargs) 
like image 106
jpic Avatar answered Sep 23 '22 17:09

jpic