Django Blog #19: Creating Forms to Send Email

by Alex
Django Blog #19: Creating Forms to Send Email

In the last section, we created a basic blog application. Now it’s time to turn it into a fully functional blog with advanced features, such as the ability to share posts by email, add comments, tag posts, and get similar posts. Next we’ll talk about the following topics:

  • Sending email using Django
  • Creating forms and handling them in views
  • Creating forms from models
  • Integrating third-party applications
  • Building complex QuerySets

Share Posts

The first thing you need to do is give users the ability to share posts via email. Think about how you can use views, URLs, and templates to create this functionality. Now you need to figure out what you will need to enable users to share posts via email. The following is required:

  • Create a form for users to enter their name and email address, the recipient’s email, and additional comments.
  • Create a view in views.py that will process the data and send the message.
  • Add a URL template for the new view in the blog application’s urls.py file.
  • Create a template to display the form.

Creating forms in Django

Let’s start by creating the form used to share posts. Django has a built-in form framework that makes it easy to create them. With it, all you have to do is define the fields, choose their display type, and specify how they will validate the data they receive. This framework provides flexible form rendering and data processing. Django has two basic classes for building forms:

  • Form: is used to build standard forms.
  • ModelForm: allows you to create forms attached to model instances.

First, you need to create a forms.py file in the blog application folder and add the following code:

from django import forms  
  
  
class EmailPostForm(forms.Form) 
    name = forms.CharField(max_length=25) 
    email = forms.EmailField() 
    to = forms.EmailField() 
comments = forms.CharField(required=False,
			       widget=forms.Textarea)

This is your first Django form. Take a look at the code. The form was created by inheriting the Form class. Other field types were used here so that Django would check them correctly.

Forms can be anywhere in your Django project. But it is customary to save them in the forms.py

file for each application. The name field type is CharField. It’s rendered as an HTML element <input type="text">. Each field type has a default widget that defines how the field is rendered in HTML. The default widget can be overwritten with the widget attribute. The comments field uses the Textarea widget to render the <textarea> HTML element instead of the standard <input>. The validation of a field depends on its type. For example, the email and to fields are EmailFields. Both require a valid email. Otherwise, a forms.ValidationError exception will be thrown and the validation will fail. Other parameters are also taken into account: the maximum length of the name field can be 25 characters, and the comments field can be made optional with required=False. All this is taken into account when checking. The field types in this form are just a subset of the bulk. The whole list is available here: https://docs.djangoproject.com/en/2.0/ref/forms/fields/.

Related Posts

LEAVE A COMMENT