Problem statement
In this Django project, implement the three-step guest checkout - the cart, then the delivery details, then the confirmation - none of which currently remembers the step before it. Items added on the store page are gone by the time the cart renders, the details form comes back blank, the confirmation has nothing to confirm, and a guest can walk straight into step 3 with an empty cart and be told nothing about it. Abandoned attempts are worse: whatever does get written sits in the session store for the global fortnight. Carry the flow in the session, report every transition once, and leave nothing behind when the order is placed.
Requirements
Implement:
- cart, cart_add, cart_update, cart_remove, checkout_continue, checkout_review, checkout_confirm, checkout_place and checkout_back in app/views.py
- the message block in app/templates/app/base.html
The following files are already implemented and provided: app/checkout.py, app/urls.py and the four step templates. There is no model and no database row anywhere in this question: the entire flow is session state. The routes are given: / (home) and /catalog/ (catalog) answer GET and are complete, /cart/ (cart) is step 1 on GET, /cart/add/ (cart-add), /cart/update/ (cart-update) and /cart/remove/ (cart-remove) take a POST, /checkout/continue/ (checkout-continue) takes a POST and moves step 1 to step 2, /checkout/review/ (checkout-review) is step 2 on GET and moves step 2 to step 3 on POST, /checkout/confirm/ (checkout-confirm) is step 3 on GET, /checkout/place/ (checkout-place) completes on POST, and /checkout/back/ (checkout-back) goes one step back on POST. django.contrib.sessions and django.contrib.messages are both installed - their apps, their middleware and the messages context processor are all in project/settings.py - so request.session works and messages is a name every template can read. The settings deliberately keep Django's global session defaults, because the policy below is per guest. Every page extends app/templates/app/base.html, and the four step templates are read-only.
app/checkout.py is given and read-only, and holds every value this question is graded against, to be read rather than retyped:
- PRODUCTS, the price list: mug is Enamel Mug at 12, tee Cotton Tee at 25, cap Canvas Cap at 18, pen Brass Pen at 3.
- the three session keys the flow owns and nothing else, CART_KEY (checkout:cart, a dict of sku to integer quantity), ENTRIES_KEY (checkout:entries, the fields step 2 has collected) and STEP_KEY (checkout:step, one of cart, review or confirm), together as FLOW_KEYS.
- STEPS, FIRST_STEP (cart), PREVIOUS_STEP, STEP_ROUTE and ENTRY_FIELDS (name, email, address).
- MESSAGES, the level and exact text of every message the flow can report.
- FLOW_AGE, which is 900 seconds.
1. The message block in the base template
- app/templates/app/base.html loops over messages above {% block content %}, rendering one element per message inside one element carrying the class messages.
- Each message element carries the class message and that message's own level_tag - success, info, warning or error - and no other level's tag. The tags are yours; the classes are not.
- A page with nothing queued renders no message block at all, not an empty one. {% if messages %} asks whether the queue is empty without consuming it, while iterating the queue is what consumes it, so a message is shown on the page the action lands on and is gone from the next one.
- The loop belongs in the base template, once, not in the four step templates.
2. The three session keys, and what an untouched guest stores
- A guest who has not acted has none of the three keys. checkout:step is absent until the first transition writes it, and a flow with no stored step is on cart, which the reader supplies.
- A removal that removed nothing is still nothing done, so it leaves no cart key behind: a stored cart, even an empty one, is a checkout in progress as far as the policy is concerned.
3. The cart operations
- cart_add adds the posted quantity of the posted sku to the stored cart and reports added. A sku that is not in checkout.PRODUCTS, and a quantity of zero or less, store nothing and report nothing.
- cart_update sets that sku's quantity, and a quantity of zero or less removes that one item. A sku the cart does not already hold changes nothing, so an update can never introduce one.
- cart_remove drops the posted sku. Removing a sku that is not in the cart changes nothing and does not create the key.
- Emptying the cart leaves the cart key holding {}, not missing.
- Neither update nor remove reports anything. All three redirect back to step 1.
4. GET /cart/
- Renders app/templates/app/cart.html with lines, total and step, where each line is a dict of sku, name, quantity, price and line_total.
- Prices come from checkout.PRODUCTS, and total is the sum of the line totals. The form posts a unit_price and the server ignores it: a posted price cannot move a total.
5. POST /checkout/continue/ - step 1 to step 2
- Moves the flow to review, reports to_review and redirects to step 2.
- With an empty cart there is nothing to check out: report empty_cart and redirect to /cart/ instead, without moving the step.
6. /checkout/review/ - step 2, and step 2 to step 3
- GET renders app/templates/app/review.html with entries, lines, total and step, so the form comes back showing whatever this guest has already entered.
- POST stores the posted fields into the entries key. A field the form did not post keeps the value already stored; a field posted empty is stored empty, because a guest who clears the name box is clearing the name rather than declining to mention it.
- Once every one of checkout.ENTRY_FIELDS holds something, the flow moves to confirm, reports to_confirm and redirects to step 3. While any field is still empty, report no_details and redirect back to step 2.
- Either way the cart guard comes first: step 2 with an empty cart reports empty_cart and redirects to /cart/.
7. GET /checkout/confirm/ - step 3
- Renders app/templates/app/confirm.html with entries, lines, total and step.
- An empty cart sends the guest back to /cart/ with empty_cart; incomplete entries send them back to step 2 with no_details.
8. POST /checkout/place/ - completion
- Guarded the same two ways, in the same order.
- With the flow complete it removes exactly the keys in checkout.FLOW_KEYS from the session, reports placed and then cleared, and redirects to the catalog.
- Anything else in the session belongs to somebody else and comes out the other side untouched, with its value, which is why this is not session.flush().
- Afterwards the guest is back on an empty step 1: no lines, a total of 0, the step reading cart, and step 2 refused again.
9. POST /checkout/back/ - back-navigation
- Moves the flow one step back using checkout.PREVIOUS_STEP, reports that step's message (back_cart or back_review) and redirects to its page via checkout.STEP_ROUTE.
- Every stored entry survives the trip: the cart is still the cart, and the details are still in the form when the guest comes forward again.
- On step 1 there is no previous step, so nothing moves, nothing is reported, and the guest lands on /cart/.
10. The expiry policy, per guest
- Every page you render leaves this guest's session carrying the policy: checkout.FLOW_AGE seconds while any flow key is stored, and browser-close once none is.
- Browser-close means the session cookie goes out with no Max-Age and no Expires, so the browser drops it when it quits. The policy age means a cookie carrying both.
- The policy is stamped after the request has finished changing the session, because whether a flow is in progress is exactly what decides which of the two ages applies.
- Nothing is graded by reading a setting back: the tests use get_expiry_age() and the attributes of the Set-Cookie header, and the age must not be the global SESSION_COOKIE_AGE.
Notes
- A session is saved at the end of the request only when it has been told it changed. Assigning a key tells it; changing a dict that is already inside it does not, so a cart read out, mutated and never assigned back looks right for the rest of that request and comes back short on the next one. request.session.modified = True is the other way to say it.
- Reading supplies the default rather than writing one in, which is what keeps "has this guest started?" a question you can ask.
- set_expiry takes three kinds of argument and the last two are the trap: a number of seconds is a lifetime from now, 0 is browser-close, and None deletes the per-guest expiry so the global fortnight applies again.
- Only app/views.py and app/templates/app/base.html change, and the expiry is set per guest with set_expiry rather than by editing project/settings.py.
- No model, no module-level dict and no hidden form field carrying state between requests: a module-level dict would be shared by every guest on the server.
- Use django.contrib.messages; do not hand-roll feedback in a session key or a query string, and style each message from its own level rather than from its text.
- This is a guest checkout: never ask who the visitor is, and never gate a step behind a login.
