In-Class Activity 5: Retrieving DB Objects:

Objective: Create a Python function to retrieve topic objects from the database and display them on a web page that utilizes template inheritance, ensuring a consistent layout through parent and child template relationships.

Description: Build a web page that lists all topics. The topics page should show all topics that users have created. Specify a URL pattern, write a view function and write a template.

  • Make a copy of your Activity4 folder and name the folder Activity5-RetrievingDBObjects. After the activity is completed, take a screenshot matching the solution image below and submit it to the first quiz question on Canvas.
  • Navigate to the directory that contains manage.py and open the folder in VS code.

  1. Start by creating the Parent template and then the Child template.

    • a. Create the parent template, base.html in the same directory as index.html.
    • b. For the Child template, rewrite index.html to inherit from base.html
      • The re-written index.html should inherit from the parent template.
  2. Create the Topics page

    • a. Define the URL for the topics page.
    • b. Define the topics() function to get some data from the database and send it to the template. Here you will need to modify the views.py file.
    • c. Create a file called topics.html in the same directory as index.html to display the topics in the template.
    • d. Finally, modify the base template to include a link to the topics page. Therefore, you will need to modify the base.html file for this last step.
  • When you refresh the home page in your browser, you’ll see a Topics link. When you click the link, you should see a page that looks similar to the below:

Web browser window on a local development site at 127.0.0.1:8000/topics/. The page shows a navigation line with Code Journal and Topics as links, then a heading Topics, followed by a bulleted list with Data_Types and Lists. The layout is simple on a light gray background with lots of white space. The tone is clean, calm, and instructional.