Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

python - Updating context data in FormView form_valid method?

I have a class QuestionView which is derived from the FormView class. Here is a 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'] = self.context_var1
        ...
        return context

As shown above, I update a set of context variables in form_valid and I intend to use the updated values of these in the template - hence the variables in the 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 the form_valid method. Is there is a workaround for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I do this with form_invalid. Here's how I do it:

from django.views.generic import FormView

class ContextFormView(FormView):
    def get(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        context = self.get_context_data(**kwargs)
        context['form'] = form
        return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form, **kwargs)

    def form_invalid(self, form, **kwargs):
        context = self.get_context_data(**kwargs)
        context['form'] = form
        return self.render_to_response(context)

You could do the same but for form_valid. Normally the body of form_valid looks like this:

def form_valid(self, form):
    return HttpResponseRedirect(self.get_success_url())

You would have to override both post and form_valid, because post calls form_valid.

def post(self, request, *args, **kwargs):
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    if form.is_valid():
        return self.form_valid(form, **kwargs)
    else:
        return self.form_invalid(form, **kwargs)

def form_valid(self, form, **kwargs):
    # take some other action here
    return HttpResponseRedirect(self.get_success_url())

oh and just to clarify, the reason this problem exists is that the ProcessFormView class's get method is broken. It normally looks like this:

def get(self, request, *args, **kwargs):
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    return self.render_to_response(self.get_context_data(form=form))

It just throws the kwargs away (._.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...