Utilizing the save method of the form

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.

Getting ready

We will build upon the example that is defined in the Passing HttpRequest to the form recipe.

How to do it…

To complete this recipe, execute the following two steps:

  1. From Django, import the function in order to send an e-mail. Then, add the save() method to MessageForm. It will try to send an e-mail ...

Get Web Development with Django Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.