Problem statement
In this Django project, implement the cart that anonymous visitors keep between page changes. The store page lists four products and every "Add to cart" button works, but the three views behind those buttons do nothing except redirect and the cart page is hard-wired to an empty list and a total of zero. Nobody is signed in and nobody is going to be, so there is no row to hang a cart on: keep it in the session, and make the three operations agree on one stored structure.
Requirements
Implement:
- cart_detail, cart_add, cart_update and cart_remove in app/views.py
The following files are already implemented and provided: app/catalog.py, app/urls.py and both templates. app/catalog.py holds PRODUCTS, a dict of sku to {"name": ..., "price": ...} with whole-number prices - mug 12, tee 25, cap 18, pen 3 - and that dict is the only place a price comes from. It also holds CART_SESSION_KEY, whose value is cart: the session key is part of the contract the tests compare against, so it lives in a file nobody edits and is imported from there. The routes are given: / (home) and /catalog/ (catalog) are complete, /cart/ (cart) renders cart_detail on GET, and /cart/add/ (cart-add), /cart/update/ (cart-update) and /cart/remove/ (cart-remove) take a POST, the first two with sku and quantity and the last with sku. The templates are read-only: catalog.html loops over products, and cart.html loops over lines and renders total, where each line is a dict with sku, name, price, quantity and line_total. The three operation views already end with return redirect("cart"), so every operation is a POST that redirects to a fresh GET of the cart page; that redirect stays, and it is also why a change that was not saved is invisible by the time the page renders. The forms post a hidden unit_price alongside the sku, as real storefront markup often does, and it is there to be ignored. posted_quantity(request, default) is given and complete: it returns the posted quantity as an int, or default when the field is missing or is not a number.
1. The stored structure
- The cart lives in request.session under CART_SESSION_KEY, as a dict of sku to quantity and nothing else: ``python {"mug": 2, "pen": 1} ``
- No prices, no names, no totals are stored; all three are worked out from PRODUCTS when the page renders.
- The key appears the first time something is actually added. A visitor whose only action changed nothing walks away with no stored cart at all.
2. POST /cart/add/
- Adds the posted quantity of the posted sku, defaulting to 1 when no quantity was posted.
- A sku already in the cart has that quantity added to its own, so adding 3 mugs to 2 mugs leaves 5.
- A sku that is not in PRODUCTS changes nothing, and so does a quantity of zero or less.
3. POST /cart/update/
- Sets the posted sku to the posted quantity, so updating 2 mugs to 4 leaves 4 and not 6.
- A quantity of zero or less removes that item and leaves the rest of the cart alone.
- A sku that is not in the cart changes nothing.
4. POST /cart/remove/
- Drops the posted sku.
- Removing a sku that is not in the cart changes nothing, and on a visitor with no cart at all it leaves no cart key behind.
- Removing the last item leaves the key in place, holding an empty dict.
5. GET /cart/
- Builds one line per stored sku in lines, each carrying that sku's name and price from PRODUCTS, its stored quantity, and a line_total of price times quantity.
- total is the sum of the line totals.
- Both are worked out from the session and the price list on every request, so a quantity update is reflected in the next render. The posted unit_price cannot move either of them.
6. The writes survive the request they were made in
- Each of the three operations is still there on the next request. request.session behaves like a dict, but the middleware saves it at the end of the request only when session.modified is true, and Django sets that flag on assignment and deletion, not on a change made to an object that is already inside the session.
- Assigning the cart back to its key, or setting request.session.modified = True, both say this; leaving a change sitting in a dict the middleware will never look at is the bug, and a quiet one, because the response rendered in that same request still shows the new value from the in-memory copy.
Notes
- Only app/views.py changes.
- Reading is free of consequences: request.session.get(CART_SESSION_KEY, {}) neither creates the key nor flags anything, so a view that decides there is nothing to do can leave the session exactly as it found it.
- A session belongs to one browser, so two visitors reading the same key read two different carts and neither can see the other's. A module-level dict would be shared by every visitor on the server.
- Prices come from PRODUCTS. The posted unit_price is not to be trusted and not to be used.
- Do not reach for the cache framework, and do not add a model, a migration or a login gate: the visitor is anonymous.
- Leave posted_quantity, catalog and the three redirect("cart") returns as they are.
