Menu
CoddyTech

Permission Required

Part of the Users and Accounts section of Coddy's Django journey. Lesson 13 of 16.

The new page /all/ lists every user's notes, for the people who answer support requests. is_staff is one switch for everything; this page needs a finer rule.

For every model, migrate creates four permissions: add, change, delete and view. Each is named by the app label and a code name, like shop.change_product. You give them to a user or a group on the user's page in the admin. A superuser has them all.

A decorator checks one:

from django.contrib.auth.decorators import permission_required


@permission_required("shop.change_product", raise_exception=True)
def edit_prices(request):
    ...

Without raise_exception=True, a user without the permission is sent to the login page, though they are logged in. With it, they get 403. In a view, request.user.has_perm("shop.change_product") asks the same; in a template, {% if perms.shop.change_product %}.

challenge icon

Challenge

Medium
  • Only users with the permission notes.view_note can open /all/. Others get 403 Forbidden, not the login page.
  • After the Stats link, a link All notes to it, shown only to users with that permission.

Press Run and open /all/ as carol (has it) and bob (does not). Password coddy-pass-123.

Try it yourself

from django.contrib.auth import login
from django.contrib.auth.decorators import login_not_required
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404, redirect, render

from .forms import NoteForm
from .models import Note


def note_list(request):
    notes = Note.objects.filter(owner=request.user)
    return render(request, "notes/note_list.html", {"notes": notes})


def note_detail(request, pk):
    note = get_object_or_404(Note, pk=pk, owner=request.user)
    return render(request, "notes/note_detail.html", {"note": note})


def note_create(request):
    if request.method == "POST":
        form = NoteForm(request.POST)
        if form.is_valid():
            note = form.save(commit=False)
            note.owner = request.user
            note.save()
            return redirect("notes:detail", pk=note.pk)
    else:
        form = NoteForm()
    return render(request, "notes/note_form.html", {"form": form})


def note_edit(request, pk):
    note = get_object_or_404(Note, pk=pk, owner=request.user)
    if request.method == "POST":
        form = NoteForm(request.POST, instance=note)
        if form.is_valid():
            form.save()
            return redirect("notes:detail", pk=note.pk)
    else:
        form = NoteForm(instance=note)
    return render(request, "notes/note_form.html", {"form": form, "note": note})


def note_delete(request, pk):
    note = get_object_or_404(Note, pk=pk, owner=request.user)
    if request.method == "POST":
        note.delete()
        return redirect("notes:list")
    return render(request, "notes/note_confirm_delete.html", {"note": note})


@login_not_required
def signup(request):
    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect("notes:list")
    else:
        form = UserCreationForm()
    return render(request, "notes/signup.html", {"form": form})


def stats(request):
    if not request.user.is_staff:
        raise PermissionDenied
    context = {"user_count": User.objects.count(), "note_count": Note.objects.count()}
    return render(request, "notes/stats.html", context)


def all_notes(request):
    notes = Note.objects.select_related("owner")
    return render(request, "notes/all_notes.html", {"notes": notes})
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Users and Accounts

Practice on your own: Django playground