How it works...

The ViralVideosAppConfig app configuration class has the ready() method, which will be called when all of the models of the project are loaded into the memory. According to the Django documentation, signals allow for certain senders to notify a set of receivers that some action has taken place. In the ready() method, therefore, we import the inform_administrators() function.

Through the @receiver decorator, inform_administrators() is registered for the post_save signal, and we have limited it to handle only the signals where the ViralVideo model is sender. Therefore, whenever we save a ViralVideo object, the receiver function will be called. The inform_administrators() function checks whether a video is newly created. In that case, it sends an email to the system administrators that are listed in ADMINS in the settings.

We use templates to generate the content of the plain_text_message, the html_message, and the subject, so that we can define default templates for each of these within our app. If we make our viral_videos app publicly available, those who pull it into their own projects can then customize the templates as desired, perhaps to wrap them in a company email template wrapper.

Learn more the Django signals in the official documentation at https://docs.djangoproject.com/en/2.1/topics/signals/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.147.87