On Tue, Oct 18, 2011 at 1:57 AM, Kurtis <kurtis.mull...@gmail.com> wrote:
> Hey,
>
> I have a FormView and a Form. I want to put in some logic that will
> take place before the Form is even displayed. I know I could throw
> this in a decorator and wrap the View but there's only a couple of
> views that need this specific functionality and didn't see a need for
> a whole new decorator.

"Before the form is even displayed" is a bit of an ambiguous
statement. The form isn't displayed until it's rendered on the
client's web browser, and the rendering doesn't happen until the very
last action in a view, so putting your logic *anywhere* would result
in it being executed "before the form is displayed".

The form is *constructed* much earlier (in, unsurprisingly, the
get_form() method).

However, the "right place" for your business logic will very much
depend on what behavior you're trying to add.

The easiest way to thing about this is probably to think about the
problem in terms of what the Class Based View is replacing. The
internals of a CBV is essentially just an implementation of the
following:

def my_view(request):
    if request.method == 'POST':
        form_class = get_form_class()
        form = get_form(form_class, data=request.POST)
        if form.is_valid():
            return form_valid()
        else:
            return form_invalid()
    else:
        form_class = get_form_class()
        form = get_form(form_class)
        return render_to_response(context_data())

If you can find the appropriate place in a function-based view, just
find the analogous place in a CBV.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to