Learn Django

New Comm - Create Form

The first resource we'll create is the communication form. This form will be used to both create new communication records, as well as update existing records. The form class is a model, based on the Communications model created in the previous chapter.

Step 1: Create the Forms File

Follow these steps to create the forms file.

  1. Navigate to /.../crmeasy/crmapp/communications
  2. Create a file there named forms.py
  3. Open this file in your IDE

Step 2: Create the CommunicationForm Class

Type in the following code into forms.py.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from django import forms

from .models import Communication

class CommunicationForm(forms.ModelForm):
    class Meta:
        model = Communication
        fields = ('subject', 'notes', 'kind', 'date', 'account',)
        widgets = {
            'subject': forms.TextInput(
                attrs={
                    'placeholder':'Subject', 
                    'class':'form-control'
                }
            ),
            'notes': forms.Textarea(
                attrs={
                    'placeholder':'Add notes...', 
                    'class':'form-control'
                }
            ),
            'kind': forms.Select(
                attrs={
                    'placeholder':'Type', 
                    'class':'form-control'
                }
            ),
            'date': forms.DateInput(
                attrs={
                    'placeholder':'Date', 
                    'class':'form-control'
                }
            ),
        }

Step 3: Commit Changes

Execute these commands to commit your changes in Git.

1
2
3
4
5
# add files
(venv)$ git add .

# commit files
(venv)$ git commit -m "created communications form"

Track your progress with a free account