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. As in the first section, the 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.

  1. In notes/views.py, write profile: render notes/profile.html with the count of the user's notes.
  2. In notes/urls.py, add profile/ named profile.
  3. Create profile.html extending notes/base.html. Show the username alone in an <h1>, then You have 2 notes. Use pluralize so one note reads You have 1 note.
  4. In base.html, link Profile to it.

As ada, click Profile: You have 2 notes.

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