Updating Context Data in FormView 'form_valid' Method

1,234 views
Skip to first unread message

Abhilash Inumella

unread,
Aug 1, 2011, 6:08:53 AM8/1/11
to Django users
Dear all,

I have a class QuestionView which is derived from FormView class. Here
is the code snippet to explain my problem:

class QuestionView(FormView):
..
context_var1 = y
def form_valid (self, form):
...
self.context_var1 = x
...
def get_context_data(self, **kwargs):
...
context['context_var1'] = context_var1
...
return context


As shown above, I update a set of context variables in form_valid and
I intend to use these in the template - hence the variables in context
dictionary. The problem with this code is that the change in
context_var1 isn't seen - might be because 'get_context_data' is
called before 'form_valid' method. Is there is a work around for
this?

Regards.
Abhilash I.

Tom Evans

unread,
Aug 1, 2011, 7:43:23 AM8/1/11
to django...@googlegroups.com
On Mon, Aug 1, 2011 at 11:08 AM, Abhilash Inumella
<abhilas...@gmail.com> wrote:
> Dear all,
>
> I have a class QuestionView which is derived from FormView class. Here
> is the code snippet to explain my problem:
>
> class QuestionView(FormView):
>  ..
>  context_var1 = y

This declares a class attribute called context_var1, which initially
has the value y. When you construct an instance of this class, this is
copied to self.context_var1

>  def form_valid (self, form):
>     ...
>     self.context_var1 = x

This updates self.context_var1

>     ...
>  def get_context_data(self, **kwargs):
>    ...
>    context['context_var1'] = context_var1

Here you are using the class attribute, rather than the instance
attribute. The class attribute is never changed from y, only the
instance attribute. You want self.context_var1 here.

>    ...
>    return context
>
>
> As shown above, I update a set of context variables in form_valid and
> I intend to use these in the template - hence the variables in context
> dictionary. The problem with this code is that the change in
> context_var1 isn't seen - might be because 'get_context_data' is
> called before 'form_valid' method. Is there is a work around for
> this?

Well, you've got to call the functions in the appropriate order. Try
accessing the attribute via self, and see where that gets you.

Cheers

Tom

Daniel Roseman

unread,
Aug 1, 2011, 2:26:18 PM8/1/11
to django...@googlegroups.com
On Monday, 1 August 2011 12:43:23 UTC+1, Tom Evans wrote:
On Mon, Aug 1, 2011 at 11:08 AM, Abhilash Inumella
<abhilas...@gmail.com> wrote:
> Dear all,
>
> I have a class QuestionView which is derived from FormView class. Here
> is the code snippet to explain my problem:
>
> class QuestionView(FormView):
>  ..
>  context_var1 = y

This declares a class attribute called context_var1, which initially
has the value y. When you construct an instance of this class, this is
copied to self.context_var1


Er, no. This declares a class attribute called context_var1, full stop. No copying to instance attributes takes here at all.

>  def form_valid (self, form):
>     ...
>     self.context_var1 = x

This updates self.context_var1

Yes. Which refers to the class attribute. So all instances will see the updated value. 

>     ...
>  def get_context_data(self, **kwargs):
>    ...
>    context['context_var1'] = context_var1

Here you are using the class attribute, rather than the instance
attribute. The class attribute is never changed from y, only the
instance attribute. You want self.context_var1 here.

Yes he does, but not for that reason.
 

>    ...
>    return context
>
>
> As shown above, I update a set of context variables in form_valid and
> I intend to use these in the template - hence the variables in context
> dictionary. The problem with this code is that the change in
> context_var1 isn't seen - might be because 'get_context_data' is
> called before 'form_valid' method. Is there is a work around for
> this?

Well, you've got to call the functions in the appropriate order. Try
accessing the attribute via self, and see where that gets you.


To make this work, the OP needs to assign to `self.context_var1` in the `__init__  method.
--
DR.

Abhilash Inumella

unread,
Aug 2, 2011, 12:11:56 AM8/2/11
to django...@googlegroups.com
Thanks much for the replies.


> As shown above, I update a set of context variables in form_valid and
> I intend to use these in the template - hence the variables in context
> dictionary. The problem with this code is that the change in
> context_var1 isn't seen - might be because 'get_context_data' is
> called before 'form_valid' method. Is there is a work around for
> this?

Well, you've got to call the functions in the appropriate order. Try
accessing the attribute via self, and see where that gets you.


I tried accessing via self as well. Even that isn't solving my problem. 
To make this work, the OP needs to assign to `self.context_var1` in the `__init__  method.
Could you please explain this in more detail?

Regards.
Abhilash I.
Reply all
Reply to author
Forward
0 new messages