To make your views clean and simple, it is good practice to move the handling of the form data to the form itself whenever possible and makes sense. The common practice is to have a save()
method that will save the data, perform search, or do some other smart actions. We will extend the form that is defined in the previous recipe with the save()
method, which will send an e-mail to the selected recipient.
We will build upon the example that is defined in the Passing HttpRequest to the form recipe.
To complete this recipe, execute the following two steps:
From Django, import the function in order to send an e-mail. Then, add the
save()
method toMessageForm
. It will try to send an e-mail to the selected recipient and will fail silently if any errors occur:# email_messages/forms.py # -*- coding: UTF-8 -*- from __future__ import unicode_literals from django import forms from django.utils.translation import ugettext,\ ugettext_lazy...