Learn Django

New Account - Create View

The account_cru() view will be used to create new accounts, and edit existing account records. You'll learn how to create this functionality in two phases. The first phase is to enable the view to create new accounts. Follow these steps to set it up.

Open the /.../crmeasy/crmapp/accounts/views.py file in your IDE. First, add the imports below to the top of the file. Then add the account_cru() view to the end of the file.

 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
from django.http import HttpResponseRedirect 
from django.core.urlresolvers import reverse

from .forms import AccountForm


@login_required()
def account_cru(request):

    if request.POST:
        form = AccountForm(request.POST)
        if form.is_valid():
            account = form.save(commit=False)
            account.owner = request.user
            account.save()
            redirect_url = reverse(
                'crmapp.accounts.views.account_detail',
                args=(account.uuid,)
            )
            return HttpResponseRedirect(redirect_url)
    else:
        form = AccountForm()

    variables = {
        'form': form,
    }

    template = 'accounts/account_cru.html'

    return render(request, template, variables)

Code Review (only new/updated lines or concepts will be reviewed)

Line 1: This imports a utility used to redirect a user's browser to any URL. It will be used later in the view to direct the user to the account detail URL.

Line 2: This imports a utility that looks up a given view's URL.

Line 4: This imports the AccountForm that was created in the previous lesson. This form will be displayed on the New Account or Edit Account pages.

Line 8: This creates the account_cru() view, and passes in the required request object.

Line 10: This starts a decision tree. If the user is submitting a form that has already been filled out, then it is a POST request, and it means the user is trying to create a new account.

Line 11: This creates a form object using the imported AccountForm(). By passing in request.POST the form picks up the values that the user submitted.

Line 12: This calls the is_valid() method to check the form values to ensure they are valid.

Lines 13-15: This calls the save() method on the form. This method saves the user data to the database. However, by passing in commit=false the changes won't be committed to the database; rather just a Python object will be created. This is helpful because we can set the owner value to the request.user before saving it.

Lines 16-19: This creates the redirect_url value. The value is created using the reverse() function. This function can look up and create the proper URL of a given view.

Line 20: This marks the completion of a success account creation. This will direct the user to the account detail page.

Line 21-22: Recall back to line 10. This is the other part of that decision tree. If the request was not a POST, meaning that the user was making a GET request, then this code will assign a blank form object.

Lines 24-26: This prepares the variables dictionary.

Line 28: This determines the template to be used.

Line 28: This returns a blank new account form to the end user.

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 view"

Track your progress with a free account