Programming logic in Classed Based FormView or in the Form Itself?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Programming logic in Classed Based FormView or in the Form Itself?

Kurtis Mullins-2
Hey,

I'm trying to figure out the best way to do this. I have a FormView
and an associated Form. In my Form, I take care of all of the
validation and cleaning as far as the input goes.

However, I need to put in some more logic such as saving the user,
logging in a user, and whatever else the future holds. I can do this
in my Class Based View just fine within the is_valid() method.

The problem comes when I need to raise an exception. I'm not sure what
kind of an exception to raise or how to present it back to the user.
For example, if a user tries to create an account and the email
address is already in use, I want to be able to display that message
to them with the form.

I know that authentication is a bad example on this since most of this
really could be done from the Form. But I have a feeling that in the
future there will be places I need to actually do this stuff from the
View itself.

Are there any suggestions on a good, or conventional, way to do this?
Basically it boils down to this -- with Class Based FormViews, should
the logic for *everything* be pushed into the Form itself? Or should
it be split up between the Form (validation) and the View (everything
else) like I am trying to do?

Any examples would also be very helpful!

Thanks

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

Reply | Threaded
Open this post in threaded view
|

Re: Programming logic in Classed Based FormView or in the Form Itself?

Xavier Ordoquy
Hi,

Nothing prevents you to put your tests in the form_valid and render the same page with your errors.

Regards,
Xavier,
Linovia.

Le 22 sept. 2011 à 17:14, Kurtis a écrit :

> Hey,
>
> I'm trying to figure out the best way to do this. I have a FormView
> and an associated Form. In my Form, I take care of all of the
> validation and cleaning as far as the input goes.
>
> However, I need to put in some more logic such as saving the user,
> logging in a user, and whatever else the future holds. I can do this
> in my Class Based View just fine within the is_valid() method.
>
> The problem comes when I need to raise an exception. I'm not sure what
> kind of an exception to raise or how to present it back to the user.
> For example, if a user tries to create an account and the email
> address is already in use, I want to be able to display that message
> to them with the form.
>
> I know that authentication is a bad example on this since most of this
> really could be done from the Form. But I have a feeling that in the
> future there will be places I need to actually do this stuff from the
> View itself.
>
> Are there any suggestions on a good, or conventional, way to do this?
> Basically it boils down to this -- with Class Based FormViews, should
> the logic for *everything* be pushed into the Form itself? Or should
> it be split up between the Form (validation) and the View (everything
> else) like I am trying to do?
>
> Any examples would also be very helpful!
>
> Thanks
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> 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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: Programming logic in Classed Based FormView or in the Form Itself?

DrBloodmoney
In reply to this post by Kurtis Mullins-2
On Thu, Sep 22, 2011 at 11:14 AM, Kurtis <[hidden email]> wrote:

> Hey,
>
> I'm trying to figure out the best way to do this. I have a FormView
> and an associated Form. In my Form, I take care of all of the
> validation and cleaning as far as the input goes.
>
> However, I need to put in some more logic such as saving the user,
> logging in a user, and whatever else the future holds. I can do this
> in my Class Based View just fine within the is_valid() method.
>
> The problem comes when I need to raise an exception. I'm not sure what
> kind of an exception to raise or how to present it back to the user.
> For example, if a user tries to create an account and the email
> address is already in use, I want to be able to display that message
> to them with the form.
>
> I know that authentication is a bad example on this since most of this
> really could be done from the Form. But I have a feeling that in the
> future there will be places I need to actually do this stuff from the
> View itself.
>
> Are there any suggestions on a good, or conventional, way to do this?
> Basically it boils down to this -- with Class Based FormViews, should
> the logic for *everything* be pushed into the Form itself? Or should
> it be split up between the Form (validation) and the View (everything
> else) like I am trying to do?
>
> Any examples would also be very helpful!
>
> Thanks


I keep all but the most simple of logic (actually I try to not have
*any* logic in views or templates). It doesn't belong there. I don't
think that django's docs make a strong statement about where it should
go, but I think it should go into models (for model instance specific
logic), a model manager (for whole-table related logic) and in the
forms (for CRUD-type logic). A basic example is that I will add a
save() method to every forms.Form that I write (to mimic the
forms.ModelForm api):

class TestForm(forms.Form):
    field
    field2
    field3

    def __init__(self, dependency, *args, **kwargs):
        self.dependency = dependency
        super(TestForm, self).__init__(*args, **kwargs)

    def clean_field(self):  ....
    def clean(self): ....

    def save(self):
        self.cleaned_data['field']
        do some fancy logic

# in view:
... boilerplate
if request.POST:
     form = TestForm(dependency, data=request.POST)
     if form.is_valid():
            form.save()
            return redirect()
     # failed validation
     return render(request, template, {'form': form})

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