Class based view that inherits from a custom class based view does not see context variables in parent

Multi tool use


Class based view that inherits from a custom class based view does not see context variables in parent
I have written a Class based view which acts as a base view for several other class based views. So the other class based views is just subclassing the base view, but the subclassed views are not getting the effect of the get_context_data or form_valid functions, so the context variables set in the base view is not getting sent to the template when requests are executing using the view subclassing the base view, they are only being sent when the base view itself it used.
Class based view:
class PortfolioNewBase(CreateView):
url_name = ''
post_req = False
def form_valid(self, form):
self.post_req = True
return super(PortfolioNewBase, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(PortfolioNewBase, self).get_context_data(**kwargs)
context['profile_id'] = self.kwargs['profile_id']
context['post_req'] = self.post_req
return super(PortfolioNewBase, self).get_context_data(**kwargs)
def get_success_url(self):
return reverse(self.url_name, args=self.kwargs['profile_id'])
When creating a new class based view which is one of the views that will be using this code it does not get access to the "profile_id" or "post_req" variables for some reason, it does not get sent to the template, but if you only use the base view written above that view will send the variables so that they are available in the view.
Code for one of the class based views using this base view written above:
class PortfolioNewDividend(PortfolioNewBase):
model = Dividend
form_class = DividendForm
template_name = 'plan/portfolio/new/new_dividend.html'
url_name = 'plan:investment-info-dividends-new'
The form works and everything, but the variables in the get_context_data in the parent is apparently not being inherited for some reason which is kind of the point here and the form_valid function is not being run either, the value of the post_req on POST requests done by the PortfolioNewDividend class based view still has the value False.
Why is the PortfolioNewDividend not running the get_context_data and form_valid functions when a request is executed with that view but the functions run if you use the base clase (written above) only ?
get_context_data
context
super
You're supposed to return the updated context. What you returned is the original context, unchanged. The last line of
get_context_data
method should be return context
– Parousia
4 hours ago
get_context_data
return context
Ah yes, good point, that worked..
– exceed
4 hours ago
1 Answer
1
One super
call too many in there. Change as follows:
super
def get_context_data(self, **kwargs):
context = super(PortfolioNewBase, self).get_context_data(**kwargs)
context['profile_id'] = self.kwargs['profile_id']
context['post_req'] = self.post_req
return context # You must actually return the modified context!
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
That
get_context_data
wouldn't work at all, either with the base class or the children, because you never returncontext
but instead callsuper
again and return that.– Daniel Roseman
4 hours ago