liveBook · Manning

Request message

As explained in Django in Action

POST is the HTTP request method used to submit data to a server, commonly for creating or changing server-side state.

HTTP POST sends submitted data to a server. A common web flow starts with GET: the browser fetches a page containing a form. Submitting that form sends a POST request, letting the server validate the input and either redisplay the form with errors or continue to a new page. section 7.1.1

Figure 7.2 Logic flow for a view that handles form submission
figure

Follow the usual single-view pattern: GET creates an empty form; POST binds submitted data, validates it, and either rerenders errors or redirects after success. section 7.1.1

In a Django view, inspect request.method to choose the path. For POST, pass request.POST to the form, call is_valid(), and read cleaned_data only after validation succeeds. A successful handler can perform work—such as sending email—then redirect to a confirmation page. Invalid data falls through to render the form again, now carrying error information. section 7.1.1 section 7.6

Comment submission

A comment page uses <form method="post"> and {% csrf_token %}. Its view creates CommentForm() on GET; on POST it creates CommentForm(request.POST), validates the fields, emails the comment, and redirects to a “Comment Accepted” page. The token is not decorative confetti: it helps prevent cross-site request forgery. section 7.1.1 section 7.6

POST also creates data through APIs. A Django Ninja endpoint decorated with .post() accepts a JSON payload defined by an input schema, converts it to a dictionary, and creates a Venue object. API input and output representations may differ; for example, newly created input has no ID while returned output does. section 11.2.6

REST conventions commonly treat GET as read and POST as creation or modification. For a more specific update contract, PUT supplies all fields, PATCH supplies changed fields, and DELETE removes data. REST is loosely defined, so these conventions guide design rather than enforce it. section 11.1.1 section 11.5

How Django in Action covers it

How deep it goes
Walks through Django form branching, validation, cleaned data, redirects, CSRF, and a Ninja JSON creation endpoint.
Examples
Uses a comment form that emails validated input and a Ninja venue endpoint that creates an object from JSON.
What it assumes
Introduces POST by contrasting it with GET, then assumes Django views, forms, and basic Python syntax for implementation.
What you can do after
Apply it via Django Forms or Django Ninja to validate submitted data and create or modify server-side objects.
How it connects
Connects POST with HTTP GET, Django Forms, validation, redirects, CSRF, REST, CRUD, JSON, PUT, PATCH, and DELETE.

Also explained in

Compare how 14 books explain request message →

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.