Re: Class Based FormView - Initial Logic

2011-10-17 Thread Russell Keith-Magee
On Tue, Oct 18, 2011 at 1:57 AM, Kurtis  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.



Re: Class Based FormView - Initial Logic

2011-10-17 Thread Andre Terra
Maybe some place inside get(), dispatch() or get_form_kwargs()?


Cheers,
AT

On Mon, Oct 17, 2011 at 3:57 PM, Kurtis  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.
>
> I know I can override the form_valid and form_invalid methods. There's
> also several "get_foo" methods I see in the source. I'm just not
> really sure where I would/could put code in that will run before the
> Form is even displayed or processed (in case they just bypass my view
> and try to throw POST data right at it, which probably wouldn't matter
> because of the CSRF).
>
> Any ideas?
>
> --
> 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.
>
>

-- 
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.



Class Based FormView - Initial Logic

2011-10-17 Thread Kurtis
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.

I know I can override the form_valid and form_invalid methods. There's
also several "get_foo" methods I see in the source. I'm just not
really sure where I would/could put code in that will run before the
Form is even displayed or processed (in case they just bypass my view
and try to throw POST data right at it, which probably wouldn't matter
because of the CSRF).

Any ideas?

-- 
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.