liveBook · Manning

Two-mode view

As explained in Django in Action

A two-mode view is a single view that handles both creation of new objects and editing of existing objects.

A two-mode view handles both adding a new object and editing an existing one, avoiding two nearly identical view functions. In Django, a default ID of 0 can represent Add mode; a real object ID selects Edit mode. section 7.2.5

A practical request flow looks like this:

  • Register one route without an ID for adding and another route with an ID for editing. section 7.2.5

RiffMates seeking ads

The seeking_ad(request, ad_id=0) view displays a blank SeekingAdForm at /seeking_ad/ and prepopulates the same form at /edit_seeking_ad/<id>/ using the selected owner's ad.

How Django in Action covers it

How deep it goes
Demonstrates routes, GET and POST branching, ModelForm instances, and owner filtering, without detailing later validation or saving steps.
Examples
Uses RiffMates seeking ads, SeekingAdForm, two URL patterns, and owner-specific edit links.
What you can do after
Build a Django add/edit workflow with shared view logic, separate routes, prepopulated forms, and owner-based access control.
How it connects
Connects Django views, URL path arguments, ModelForms, ORM object IDs, authentication, and ownership checks.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.