Learn Django

View Comm - Create View

With the app, model, and URL config created, its time to code the communications detail view. This view can be used to display the full details of a communication on a standalone page. The view is simple; it accepts the uuid, queries the database for the record, verifies the user is the owner, and displays the communication on a page if everything is successful.

Open the /.../crmeasy/crmapp/communications/views.py file in your IDE and edit it as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseForbidden

from .models import Communication

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

    comm = Communication.objects.get(uuid=uuid)
    if comm.owner != request.user:
            return HttpResponseForbidden()

    return render(request, 'communications/comm_detail.html', {'comm':comm})

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

Track your progress with a free account