Menu
CoddyTech

A Profile Page

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

In this chapter you add features with what you know, and less help. First, a page of your own: who is signed in and how many notes they wrote. Everything it needs is on the signed in user: the username, and their notes through the related_name of the owner field.

To show a number of rows, ask the database for the number. On a library site, author.books.count() runs one SELECT COUNT(*). len(author.books.all()) gives the same number, but loads every book first.

English needs 1 book but 2 books. The template filter pluralize adds an s unless the number is 1: book{{ count|pluralize }}. Another ending goes in quotes: box{{ count|pluralize:"es" }}.

challenge icon

Challenge

Medium

Add a profile page at /profile/, named profile:

  • An <h1> with the username.
  • You have 2 notes. with the user's own count: You have 1 note. for one, You have 0 notes. for none.
  • In the header, after Signed in as ..., a link Profile to it.

Press Run and open /profile/ as ada, then as bob (password coddy-pass-123).

Try it yourself

from django.contrib.auth import login
from django.contrib.auth.decorators import login_not_required, permission_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)


@permission_required("notes.view_note", raise_exception=True)
def all_notes(request):
    notes = Note.objects.select_related("owner")
    return render(request, "notes/all_notes.html", {"notes": notes})

All lessons in Users and Accounts

Practice on your own: Django playground