Reader small image

You're reading from  Becoming an Enterprise Django Developer

Product typeBook
Published inJun 2022
Reading LevelIntermediate
PublisherPackt
ISBN-139781801073639
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Michael Dinder
Michael Dinder
author image
Michael Dinder

Michael Dinder works as a senior backend developer at Cart, Inc. Michael has helped to develop projects for large enterprises such as PayPal and other companies such as Corcoran Pacific Properties, and countless more either directly or indirectly. He has been programming for more than 15 years with a number of different languages and frameworks, with a focus on Python/Django for the past 5+ years.
Read more about Michael Dinder

Right arrow

Chapter 7: Working with Messages, Email Notifications, and PDF Reports

In this chapter, we will work with the Django messages framework, email notifications and templates, and PDF documents. In order for us to know that emails are actually being sent from our system and that they are rendered correctly in different email clients, we will be using a free third-party service to capture all of our outgoing emails. By capturing all outgoing emails, we can prevent development and test emails from being sent to people who should not be seeing them yet. We will use HTML, CSS, and the Django template language to create email and PDF report templates. Both will use context data just like how we passed context into templates in Chapter 4, URLs, Views, and Templates.

In this chapter, we will cover the following:

  • Creating a test environment for capturing all emails sent by the app
  • Using the Django messages framework to create flash messages and custom message levels
  • Creating...

Technical requirements

To work with the code in this chapter, the following tools will need to be installed on your local machine:

  • Python version 3.9 – used as the underlying programming language for the project
  • Django version 4.0 – used as the backend framework of the project
  • pip package manager – used to manage third-party Python/Django packages

We will continue to work with the solution created in Chapter 2, Project Configuration. However, it is not necessary to use the Visual Studio IDE. The main project itself can be run using another IDE or run independently using a terminal or command-line window from within the project root folder, which is where the manage.py file resides. Whatever editor or IDE you are using, a virtual environment will also be needed to work with the Django project. Instructions for how to create a project and virtual environment can be found in Chapter 2, Project Configuration. You will need a database to store...

Preparing for this chapter

Start by creating a new app in your project called chapter_7 by following the steps discussed in Chapter 2, Project Configuration, in the subsection titled Creating a Django app. As discussed in that section, don't forget to change the value of the name = variable for your app class found in the /becoming_a_django_entdev/becoming_a_django_entdev/chapter_7/apps.py file to now point to the path where you installed your app. Be sure to also include this app in the INSTALLED_APPS variable found in the settings.py file as well.

In the main urls.py file of the site, add the following path, which points to the URL patterns of the app that we will be creating in this chapter:

# /becoming_a_django_entdev/urls.py
...
urlpatterns = [
    path(
        '', 
        include('becoming_a_django_entdev.chapter_7.urls')
 ...

Using the Django messages framework

Let's begin by introducing the Django Messages Framework, which is a framework used to provide session-based messages to the user. A flash message is a one-time notification message displayed directly to the user and is the kind of message that this framework creates. What we can do with this is render messages to the user anywhere we put the code inside our templates, whether that be in a modal popup or a message that drops down from the top of the page or comes up from the bottom of the page. It can even appear above or below a form that the user is submitting.

The chapter_7 FormClassView class will be the primary working class throughout this chapter, as it will be used primarily to trigger the actions we will be writing. We will be writing the methods to perform those actions in the corresponding ContactForm class used by that FormClassView class of the chapter_7 app.

Before we start writing those classes, we will begin by enabling...

Configuring email notifications

This section will help us to build actual email notifications instead of flash messages. We will write our logic to trigger the send email action in the same FormClassView class where we added messages inside the post() method. We will be utilizing the Mailtrap account that we created at the beginning of this chapter to capture all emails that are sent by our project. If you have not already done so, please create an account with Mailtrap and configure that connection in your settings.py file. Without doing so, you will have difficulty executing the code throughout this section.

There are three MIME types that exist for emails, as follows:

  • Plain texttext/plain
  • Rich textapplication/rtf
  • HTMLtext/html

While there are three MIME types, only two are used by Django when sending emails: plain text and HTML. Rich text emails are treated as HTML emails because they contain HTML markup.

As plain text emails...

Writing custom email templates

Writing HTML as a string in Python can get really messy. We can write the body contents, such as '<b>Hello World</b>', as a .html template file instead. That will allow us to organize multiple email templates into the /templates/emails/ directory of the chapter_7 app. Programming work can also be shared among developers in this way. Email templates can also be used for plain text-formatted emails, placing only the text without any HTML code inside of that .html file. While that may not sound appealing for plain text emails, this does have its benefits when working among a large team of developers. Let's begin with the simplest template using only plain text emails.

Django provides the get_template() method, found in the django.template.loader library. This method will be used for all the email template examples in the following subsections.

For plain text emails

Follow these steps to create a template for a plain text...

Generating PDF reports

Django relies on the support of third-party packages in order to generate PDF documents. Their own documentation even suggests using the reportlab package; however, any third-party package that provides PDF support can be used. When using anything other than reportlab, refer to that package's documentation for instructions on how to use that package. The reportlab package even provides sample PDF invoices, reports, catalogs, and more for developers to get started quickly and easily, that is, if they are using the paid Plus version of the reportlab package. The Plus version requires the rlextra package, which is not available to the public. To learn more about what this service and package can provide, visit their documentation at https://www.reportlab.com/dev/docs/.

For the exercises throughout this section, we will be using the xhtml2pdf package instead, which is also free but a bit simpler and easier to use when working with template-based PDFs. We...

Summary

With the skills gained after completing the exercises found in this chapter, you can now create and send messages, notifications, and reports of various types. We now know how to use the Django messages framework to serve up flash messages every time a page loads or reloads. We can create and send emails of various content types and even use an email test client account to capture those emails, indicating that they are actually working. We even installed a package and began building our own PDF reports.

Use any combination of these tools to add value to your project. Flash messages, email notifications, and report generating concepts all help to keep users informed and engaged with your application. Always remember that too much information can overwhelm a user, such as having thousands of email notifications flooding their inbox. Use them wisely!

The Django messages framework offers a wide range of tools that can create flash messages for users. With a little bit of...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Becoming an Enterprise Django Developer
Published in: Jun 2022Publisher: PacktISBN-13: 9781801073639
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Michael Dinder

Michael Dinder works as a senior backend developer at Cart, Inc. Michael has helped to develop projects for large enterprises such as PayPal and other companies such as Corcoran Pacific Properties, and countless more either directly or indirectly. He has been programming for more than 15 years with a number of different languages and frameworks, with a focus on Python/Django for the past 5+ years.
Read more about Michael Dinder