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 = yThis 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 = xThis updates self.context_var1
> ...
> def get_context_data(self, **kwargs):
> ...
> context['context_var1'] = context_var1Here 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.
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/ZrHadRZnil0J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment