Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Request Data Extract | Request Handling
Django: First Dive
course content

Зміст курсу

Django: First Dive

Django: First Dive

1. Get Started
2. Write the First Page
3. Models
4. Working with Database
5. Templates
6. Request Handling

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 variable html. 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 a for loop.
  • For each key-value pair, it appends an HTML paragraph element (<p>) to the html 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.

Все було зрозуміло?

Секція 6. Розділ 3
We're sorry to hear that something went wrong. What happened?
some-alt