Search⌘ K
AI Features

Writing the Code for the Views

Explore how to implement the home page view in a Django e-library app by handling form submissions, validating form data, saving entries to the database, and displaying dynamic content using Django templates. Understand how to adjust test classes to accommodate database connections and ensure all tests pass successfully.

In this lesson, we add the code for the home page view. To do this, we need to perform the following steps:

  1. We need to submit the data received from the form to the database if the form inputs are valid.
  2. We need a way to present the form data on the home page.
  3. We need to display the empty form for users to enter a new data entry. This has been implemented before, so we’ll cover the first two.

The code for the views

Here’s the complete code for the views:

The Django views

We add some code in the views.py file. Line 11: We check if the form has been submitted.

Line 13: If it has been submitted, we get data from the form the user filled.

Line 16: We check if the form the user filled is valid using the is_valid() function. ...