Staff Only
Part of the Users and Accounts section of Coddy's Django journey. Lesson 12 of 16.
The new page /stats/ counts users and notes. It is for the people who run the site, and any signed in user can open it.
Being signed in is one question; what a user may do is another. User has a flag for the people who run a site: is_staff, true for those who may use the admin.
When a signed in user asks for something they may not see, the response is 403 Forbidden: the site knows who you are, and says no. In a view, raise PermissionDenied and Django sends the 403. A forum refuses replies to a locked thread:
from django.core.exceptions import PermissionDenied
def thread_reply(request, pk):
thread = get_object_or_404(Thread, pk=pk)
if thread.locked:
raise PermissionDenied
...In a template the flag is on user, so a link can show only to the people allowed to follow it. The link is a convenience; the view's check is the protection.
Challenge
EasyKeep /stats/ for staff.
- In
notes/views.py, makestatsrespond with 403 Forbidden to any user who is not staff. - In
base.html, afterNew note, add a linkStatsto/stats/, shown only to staff.
Open /stats/ as ada (staff): you see the counts. As bob: 403 Forbidden. 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.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):
context = {"user_count": User.objects.count(), "note_count": Note.objects.count()}
return render(request, "notes/stats.html", context)
All lessons in Users and Accounts
Practice on your own: Django playground