Learn Django

New Account - Create Form

Users can create new accounts by going to the /account/new/ URL. This URL will display a blank form in which a user can enter a company name, description, and address. In this lesson you'll create the form that is used to create new accounts.

Step 1: Create the Forms File

Follow these steps to create the forms.py file.

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

Step 2: Create the AccountForm 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from django import forms

from .models import Account

class AccountForm(forms.ModelForm):
    class Meta:
        model = Account
        fields = ('name', 'desc', 'address_one',
                  'address_two', 'city', 'state', 'phone',
        )
        widgets = {
            'name': forms.TextInput(
                attrs={
                    'placeholder':'Company',
                    'class':'col-md-12 form-control'
                }
            ),
            'desc': forms.Textarea(
                attrs={
                    'placeholder':'Enter a description',
                    'class':'form-control'
                }
            ),
            'address_one': forms.TextInput(
                attrs={
                    'placeholder':'Street Address',
                    'class':'gi-form-addr form-control'
                }
            ),
            'address_two': forms.TextInput(
                attrs={
                    'placeholder':'Suite, PO, etc',
                    'class':'gi-form-addr form-control'
                }
            ),
            'city': forms.TextInput(
                attrs={
                    'placeholder':'City',
                    'class':'gi-form-addr form-control'
                }
            ),
            'state': forms.TextInput(
                attrs={
                    'placeholder':'State',
                    'class':'gi-form-addr form-control'
                }
            ),
            'phone': forms.TextInput(
                attrs={
                    'placeholder':'Phone',
                    'class':'gi-form-addr form-control'
                }
            ),
        }

Code Review (only new/updated lines, and new concepts are taught)

This code should look similar to the Subscriber form that was created earlier. Instead of stepping through it line by line I'll reiterate a few points.

  • This is a ModelForm form, meaning that it's based on a model, in particular the Account model.
  • We use the fields attribute to declare which fields we want to be shown in the rendered HTML page.
  • The widgets attribute is used to set two HTML attributes; placeholder and class. Setting the placeholder attribute allows the form to be displayed with helpful text in the boxes. The class attribute allows CSS classes to be assigned, which assists with making the form look nice.

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 "added new account form"

Track your progress with a free account