StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 7 · Class-Based Views

Notes CRUD Application

mediumDjango Class-Based ViewsAccess Control75%3 of 4 solved

Problem statement

The notes app has five routes and five generic views, and every one of them is an empty shell. Nothing is gated, so anyone can read and delete anything. Nothing is scoped, so everyone's notes are in everyone's list. The list hands over every note at once. The create view has no form to build and no owner to attach, and the delete view has nowhere to go once the note is gone. Build the whole CRUD surface out of what the generic views already do, and the few things they cannot know.

Requirements

Implement:

  • get_absolute_url on Note in app/models.py
  • the five views in app/views.py: NoteListView, NoteDetailView, NoteCreateView, NoteUpdateView and NoteDeleteView

The following files are already implemented and provided: app/forms.py, app/urls.py and every template. Note holds title, slug (unique), body, created_on and an owner pointing at a user. NoteForm covers those four fields and not the owner, because who a note belongs to is not the browser's to send. The routes are given: /accounts/login/ is login, /notes/ is note-list, /notes/new/ is note-create, /notes/<slug>/ is note-detail, /notes/<slug>/edit/ is note-edit and /notes/<slug>/delete/ is note-delete. LOGIN_URL is /accounts/login/. The list template is app/notes.html and the rest sit at the names Django's conventions expect - app/note_detail.html, app/note_form.html and app/note_confirm_delete.html - so only the list needs a template_name. The list template loops over notes and reads page_obj, is_paginated and note_total. Each of the five views is declared as an empty shell, with no model yet.

1. The note's own address

  • Note.get_absolute_url() returns the URL of that instance's detail page, built from the note-detail route name and the instance's own slug.
  • This is where CreateView and UpdateView send the browser after a save: they ask the saved object itself.

2. The login gate

  • All five views require a signed-in visitor. An anonymous GET of any of the five answers 302 to a Location starting with /accounts/login/, carrying next= the page that was asked for, and an anonymous POST creates and deletes nothing.
  • The gate has to run before anything reads request.user.

3. The ownership scope

  • List, detail, edit and delete see only the signed-in user's notes, so another user's list never shows them and note_total never counts them.
  • Someone else's note answers 404 on GET and on POST, for detail, edit and delete alike, and leaves it unchanged and undeleted. That 404 is indistinguishable from the one for a slug that never existed; a 403 would confirm the record exists.
  • Narrow what the parent already fetched rather than building a fresh queryset: ListView applies ordering inside its own get_queryset, so a fresh Note.objects.filter(...) would quietly throw the ordering away.

4. The list page

  • Renders app/notes.html and publishes the list in the context as notes.
  • Newest first by created_on, with title breaking ties, so a note titled Aardvark sharing the newest date leads Note 06.
  • Five notes per page. The context then carries page_obj, paginator and is_paginated, ?page= is read by the view itself, and a page past the end is a 404 rather than a fallback page of rows.
  • The context also carries note_total: the count of all the signed-in owner's notes, not just the page, which the template renders as its count line and which follows a deletion.

5. Create

  • NoteCreateView builds NoteForm, so GET /notes/new/ answers 200 with an instance of it, unbound.
  • A valid POST saves the note and redirects to the new note's own page. An invalid one comes back as a rendered 200 page with the error on its field - a missing title reported under title - and saves nothing.
  • The new note's owner is the signed-in user, taken from the request, so an owner the browser posts anyway does not decide it.

6. Update and delete

  • NoteUpdateView builds NoteForm too - an update view with no form is as unconfigured as a create view with none - and a successful edit saves the change, leaves the owner as it was, and redirects to the note's own page.
  • NoteDeleteView renders app/note_confirm_delete.html on GET and changes nothing, removes the note only on POST, and then redirects to /notes/, built from the note-list route name. It has no object left to ask, so that target is spelled out at class level, where the URLconf is not loaded yet.

Notes

  • Only app/views.py and app/models.py change.
  • Narrow the queryset; do not fetch an object and then test it. A stranger's note must be absent, not forbidden.
  • Call super() in get_queryset and get_context_data, or you will lose what the parent did.
  • The owner comes from the request, never from the form. Do not add owner to NoteForm.
  • Build every URL from its route name rather than writing paths as strings. Both work today; only one survives a change of route.
  • Do not override get, post or delete, do not set fields, and do not paginate by hand.

More in Class-Based Views

See the module
Paginated Article ListConfigure a Django ListView with its template, context name, ordering and built-in pagination.100%3 of 3 solvedmediumArticle Detail And CreateWire a DetailView and a CreateView with a form class, redirecting to the new object through get_absolute_url.100%3 of 3 solvedmediumScoped Edit And DeleteRestrict UpdateView and DeleteView to the signed-in user's own objects by overriding get_queryset, with login required and defined redirects.100%3 of 3 solvedmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.