Learn Django

Edit Account - Modify View

The next step is to update the view. Luckily the same view can be used for both creating new accounts and editing existing ones. Some changes need to be made in order for this to work.

The first change that needs to be made is to update the view so that it accepts the uuid as a value. We'll give it a default value of None which so that the view can still be used to create new accounts. When using the view to edit accounts, the uuid value will be replaced with the ID in the URL.

The second change is to add the block of code, starting with the if uuid: statement. If there is a uuid, this section will fetch the object from the database. If there isn't a uuid a new account instance will be created.

Open /.../crmeasy/crmapp/accounts/views.py file in your IDE. Locate the account_cru() view and update it as follows. Don't forget to add the additional import to the top of the file!

New or modified lines are highlighted in yellow.

 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
from django.shortcuts import get_object_or_404


@login_required()
def account_cru(request, uuid=None):

    if uuid:
        account = get_object_or_404(Account, uuid=uuid)
        if account.owner != request.user:
            return HttpResponseForbidden()
    else:
        account = Account(owner=request.user)

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

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

    template = 'accounts/account_cru.html'

    return render(request, template, variables)

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

Line 1: This imports a utility that attempts to search a given model for a record. If the record does not exist, then a 404 error is generated.

Line 4: A new optional argument is now passed into the view; uuid. Passing a uuid into the view will signal that the view will be used to edit an existing account. If no uuid is passed, the view will proceed as if it's creating a new account.

Lines 6-11: This block of code attempts to look up the account. This only executes if the uuid is passed into the view. If the uuid is passed in, an attempt is made to look up the account. If no uuid is passed, then the account variable is assigned to a new account instance.

Line 14: We are modifying this line to include the instance=account object when creating the form object.

Line 16: This one line should replace the three previous lines.

Line 23: Modify this line to include instance=account.

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 "modified new account view to allow for edits"

Track your progress with a free account