liveBook · Manning

Two-route design

As explained in Django in Action

A two-route design maps separate add and edit URL patterns to the same view.

A two-route design sends separate add and edit URLs to one view. The add route supplies no ID; the edit route supplies the object ID. section 7.2.5

The view distinguishes its work by the ID it receives:

  • Use a default ID of 0 for Add mode, then create an empty form on GET or a bound form on POST. section 7.2.5
  • Use the supplied ID for Edit mode, fetch the object, and pass it as the ModelForm instance on both GET and POST. section 7.2.5

How Django in Action covers it

How deep it goes
Explains route registration, mode selection, GET and POST form construction, object loading, and ownership filtering.
Examples
Uses SeekingAd creation and editing with seeking_ad/ and edit_seeking_ad/<int:ad_id>/ routes.
What it assumes
Assumes basic Django views, URL patterns, forms, ORM objects, authentication, and HTTP GET and POST handling.
What you can do after
Build it from scratch by routing add and edit requests into one view with correctly populated forms.
How it connects
Connects URL patterns, views, path arguments, ModelForm instances, ORM lookups, and object-level authorization.

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.