Course Content
Django: First Dive
Django: First Dive
Request Data Extract
Let's continue to prescribe the logic of creating posts. Everything is ready to get the received data from the forms for our posts.
The request.POST
is a dictionary with received data (context). We can operate with this as regular dict
in Python. Let's iterate on request.POST
dictionary and output this information as HTML page:
Let's break down what the code does. If the request method is "POST"
, the code executes the following steps:
- It initializes an HTML string with the content
"<h1>Complete!</h1>"
and assigns it to the variablehtml
. This string will serve as the response content. - It retrieves the data submitted with the POST request using
request.POST
, which is a dictionary-like object containing the submitted form data. - It iterates over each key-value pair in the
data
dictionary using afor
loop. - For each key-value pair, it appends an HTML paragraph element (
<p>
) to thehtml
string, displaying the key and its corresponding value. - Finally, it returns an HTTP response with the
html
string as the content. This will display the submitted form data as paragraphs below the "Complete!" heading.
Result
GET
The method that is passed when the page is accessed via the URL.
Some data for the post has been entered.
POST
This method is passed when the submit button is clicked (if method="POST"
is specified in the form).
Now you can see the data passed in the HTML form. The request.POST
dictionary has information about new post and CSRF Token.
Keys Naming
The keys in the request.POST
dictionary named inside the HTML template:
In the given HTML code, the attribute names used have the following meanings:
-
name="title"
: Provides a name for the<input>
element. When the form is submitted, the data entered into this field will be sent with the specified name. -
name="text"
: Provides a name for the<textarea>
element. When the form is submitted, the data entered into this field will be sent with the specified name.
These attributes and their values are used to define the data associated with the form and its input fields.
Thanks for your feedback!