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


I am trying to get Django class based ideas (CBV).

BlahView (TemplateView): Template_name = 'blah / blah.html' def get_context_data (self, ** kwargs): #code ... def find (self, request, ** kwargs ): # More code ...

Now, I know that I can get request advice from myself. Now to say that I want to parse the parameters of these requests and want them to be stored in the classroom. Can I store it in self.xxx ? Now, obviously how classes work, it is considered directly.

But I can not exclude the flow of control by looking at the definition of see > TemplateView ). as_view () refers to being an 'entry point'

I think about setting the example variable in the beginning of get_context_data () I do not think the right to initialize.

Can I define a __init __ () for my CBV? If so, can there be a threading issue or something like where many page-access probably work with a global example of my parsed data?

I know that this is a bit messy, but I'm just a bit confused of code flow in CBV.

According to django.views.generic.base.View

  • On the default setting Li>
  • On request , the view () is called , it immediately calls and calls to Dispatch )
  • Class installation thread is protected
  • According to django.views.generic.base.view .__ init __ , The request object is out of the scope at this point So that you can not parse it in your own constructor overload.

    However, you can parse the request and set the class view example attributes in a surcharge django.view S.generic.base.View.dispatch , it According to the following:

      class YourView (SomeView): def send (self, request, * args, ** kwargs): # request here i.e. Self.foo = request.GET.get ('foo', False) # Returns super call (see yours, self). Dispatches (requests, * args, ** kwargs)  

    Comments

    Popular posts from this blog

    Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

    java - How not to audit a join table and related entities using Hibernate Envers? -

    mongodb - CakePHP paginator ignoring order, but only for certain values -