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

Scoped Edit And Delete

mediumDjango Class-Based ViewsAccess Control100%3 of 3 solved

Problem statement

The edit and delete pages fetch an article by the slug in the URL and then do exactly as they are told. Change the slug and you are editing someone else's work; there is not even a sign-in to get past. The fix is not a permission check bolted on afterwards - it is making ownership part of the lookup, so an article that is not yours was never found in the first place.

Requirements

Implement:

  • the gate and the scope on ArticleUpdateView and ArticleDeleteView in app/views.py

The following files are already implemented and provided: app/models.py, app/forms.py, app/urls.py and every template, including the delete confirmation at app/article_confirm_delete.html. Article holds title, slug, body, published_on, an author pointing at a user, and a get_absolute_url returning its detail page. The routes are given: /accounts/login/ is login, /articles/ is article-list, /articles/<slug>/ is article-detail, /articles/<slug>/edit/ is article-edit and /articles/<slug>/delete/ is article-delete. LOGIN_URL is /accounts/login/. ArticleForm covers title, slug, body and published_on, and not the author: ownership is decided by who is signed in, never by what the browser sends. ArticleUpdateView already names its model and form_class, and ArticleDeleteView its model.

1. The login gate

  • Both views require a signed-in visitor. An anonymous GET of either page answers 302 to a Location starting with /accounts/login/, carrying next= the page that was asked for, and an anonymous POST to the delete page changes nothing.
  • The gate has to run before anything tries to read request.user.

2. The ownership scope

  • Both views see only articles whose author is the signed-in user, so the owner's own edit page answers 200 with the form bound to the article in the URL, and the owner's delete POST removes it.
  • Another author's article answers 404, on GET and on POST, for edit and for delete alike, and leaves that article unchanged and undeleted.
  • That 404 is indistinguishable from the 404 for a slug that never existed. A 403 would be a worse answer: it confirms the record exists, which is information a stranger should not have.
  • Make ownership part of the lookup rather than a check that runs after the object is fetched, and narrow what the parent already fetched rather than building a fresh queryset of your own: the parent may have applied things you would otherwise discard.

3. The redirects, and delete asking first

  • A successful edit redirects to the article's own page.
  • A successful delete redirects to /articles/, built from the article-list route name. DeleteView has no object left to redirect to, so unlike CreateView and UpdateView it needs that target spelled out - at class level, where the URLconf is not loaded yet.
  • GET on the delete route renders app/article_confirm_delete.html and changes nothing; only POST removes the article.

Notes

  • Only app/views.py changes.
  • Narrow the queryset; do not fetch the object and then test it. A stranger's article must be absent, not forbidden.
  • Build the delete redirect from the route name rather than a typed path: it works either way today, but only one survives a change of route.
  • Do not override get, post, delete or form_valid.

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 solvedmediumNotes CRUD ApplicationBuild a complete CRUD surface with generic class-based views: paginated owner-scoped list with extra context, detail, create attaching the owner server-side, and scoped update and delete behind a login gate.75%3 of 4 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.