Learn Django

New Comm - Create View

We already mentioned that users will be able to create new communication records by accessing /comm/new/. However, per the application design, they will also be able to add records from the account detail page. In fact, part of the new communications form is showing on the account detail page.

That means we need to do two things. First, we need to create the view that will handle the creation of new communication records. Then, we need to update the account detail view to create an instance of the communication form so that the page renders it when loaded.

Step 1: Create the Communications View

The comm_cru() should look familiar. It follows the same pattern as previous views you've created. First, the function looks to see if the request is a HTTP POST. If it is then the view assumes the user is trying to create a new communication record. The view will verify the user is the owner, and then proceed to save the view. If all works well then the user is returned back to the account detail page.

If the request is a HTTP GET, then the view assumes the user wants to see a blank communication form.

Open the /.../crmeasy/crmapp/communications/views.py file. Start by adding the imports at the top of the file, then add the communications_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
31
32
33
34
35
36
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from .forms import CommunicationForm  


@login_required()
def comm_cru(request):

    if request.POST:
        form = CommunicationForm(request.POST)
        if form.is_valid():
            # make sure the user owns the account
            account = form.cleaned_data['account']
            if account.owner != request.user:
                return HttpResponseForbidden()
            # save the data
            comm = form.save(commit=False)
            comm.owner = request.user
            comm.save()
            # return the user to the account detail view
            reverse_url = reverse(
                'crmapp.accounts.views.account_detail',
                args=(account.uuid,)
            )
            return HttpResponseRedirect(reverse_url)
    else:
        form = CommunicationForm()

    variables = {
        'form': form,
    }

    template = 'communications/comm_cru.html'

    return render(request, template, variables)

Step 2: Add the Communications Form to the Account Detail

As mentioned, users will be able to add communications from the account detail page. That means we need to render part the form in the template.

Open /.../crmeasy/crmapp/accounts/views.py and update the account_detail() view as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from crmapp.communications.forms import CommunicationForm

@login_required()
def account_detail(request, uuid):

    account = Account.objects.get(uuid=uuid)
    if account.owner != request.user:
            return HttpResponseForbidden()

    contacts = Contact.objects.filter(account=account)
    communications = Communication.objects.filter(
        account=account).order_by('-created_on')

    form = CommunicationForm()

    variables = {
        'account': account,
        'contacts': contacts,
        'communications': communications,
        'form': form
    }

    return render(request, 'accounts/account_detail.html', variables)

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 the new communication view"

Track your progress with a free account