StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 3 · URLs & Views

Safe Detail And JSON Views

easy100%3 of 3 solved

Problem statement

A shop needs a product page and a small JSON API for the same products. The Product model, the URL routes and the detail template are provided, but both views in app/views.py are still stubs. Implement product_detail and product_json so a known product renders its page or its JSON, and a missing product answers 404 on both instead of crashing the server.

Sample Data

The provided Product model (app/models.py) has name (CharField), description (TextField, may be blank) and price (DecimalField, 2 decimal places). Example rows:

idnamedescriptionprice
1Desk LampAdjustable LED lamp39.90
2Coffee Mug(blank)8.50

Requirements

Only app/views.py needs to change. The routes in app/urls.py and the template app/templates/app/product_detail.html are provided.

1. Detail view - GET /products/<id>/ (name="product-detail")

  • Implement product_detail(request, pk).
  • For a known id, render app/product_detail.html with the product in the template context under the key product, returning 200.
  • For an id with no product (never existed, or deleted), return 404 instead of raising an unhandled error. Django has a shortcut for exactly this look-up-or-404 case.

2. JSON endpoint - GET /api/products/<id>/ (name="product-json")

  • Implement product_json(request, pk).
  • For a known id, return 200 with a JSON body that is exactly these fields and nothing else:
  • id - integer primary key
  • name - string
  • description - string (an empty string when blank)
  • price - string with two decimal places, e.g. "39.90"
  • The Content-Type header must be application/json. Do not build the JSON string by hand; values containing quotes, backslashes or newlines must still produce valid JSON.
  • For an id with no product (never existed, or deleted), return 404.
Sample Requests & Responses

GET /products/1/

200, HTML rendered from app/product_detail.html; the template context contains product (the Desk Lamp instance).

GET /products/999/

404

GET /api/products/1/

200, Content-Type: application/json

{"id": 1, "name": "Desk Lamp", "description": "Adjustable LED lamp", "price": "39.90"}

GET /api/products/2/

200, Content-Type: application/json

{"id": 2, "name": "Coffee Mug", "description": "", "price": "8.50"}

GET /api/products/999/

404

More in URLs & Views

See the module
Article RoutingOnly the home page is routed, so every article URL is a 404. Route articles by id, slug and four-digit year so the view receives typed values and malformed URLs are rejected before any view runs.100%3 of 3 solvedeasyNamed Routes And RedirectsLinks and redirects break every time a URL changes. Name the routes inside an app namespace, build every link from those names, and send old post URLs to their new home with a permanent redirect.100%3 of 3 solvedeasyRecipe Site ViewsA recipe site needs its whole request layer built. Add namespaced routes for the list, detail, year archive, JSON API and a legacy redirect, with typed URL arguments, 404s for bad input, and every URL built from its route name.100%3 of 3 solvedeasy

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.