In-Class Activity 8: Adding New Entries and Displaying Data with Plotly Express:

Objective: Create a second form and add pages that allow users to enter their own data. Use Plotly Express and Pandas to create a bar chart that displays the number of entries per topic.

Description: Build a second Django form that handles GET and POST requests that allow users to add new entries which will save to the database. Also, display the number of entries per topic using Pandas & Plotly Express.

  • Make a copy of your Activity7 folder and name the folder Activity8-AddingNewEntries. After the activity is completed, take three screenshots matching the solution images 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.

Part 1: Adding New Entries

  1. Create your second form in the file forms.py. Save this new file in the same directory as models.py.
  2. Append the urls.py file (code_journals/urls.py) to add an additional URL pattern for the new_entry page. This pattern should send requests to the view function new_entry().
  3. Add a new view function, new_entry(), to handle GET and POST requests for the entry form in the views.py file. In the file, make sure to import EntryForm.
  4. Create a new template called new_entry.html to display the form. Ensure that the template extends base.html.
  5. Next, edit the topic template (topic.html) to link the new_entry page from each topic page.
  6. Finally, using the new entry form, add one entry to the Dictionaries topic.
  • The first screen capture below shows the resulting form. Use the form to add an additional entry for the Dictionaries topic. Once the entry is added, you should see a similar result displayed below in the second screen capture.

Code Journal new entry form page showing navigation breadcrumbs at the top reading Code Journal - Topics with Dictionaries linked below. The main heading states Add a new entry: followed by a large text input field for entering content. Below the text field is an add entry button. The page has a clean, minimal white background with dark gray text, displaying a welcoming and professional interface for users to contribute their own learning notes.

Code Journal Dictionaries topic page displaying the topic name with a linked breadcrumb navigation at the top. The page shows a newly added entry for Dictionaries with the content displayed as a formatted note. The interface features a clean white background with dark gray text, presenting a clear and organized view of the saved entry within the Code Journal application for CIS 240.


Part 2: Displaying Data with Plotly Express

  1. Append the urls.py file (code_journals/urls.py) to add an additional URL pattern for visualizing the number of entries for each topic. This pattern should send requests to the view function bar_chart().
  2. Define a new view function named, bar_chart. The function should get the request object and create a bar chart using the foreign key relationship, visualizing the number of entries for each topic.
  3. Create a new template called bar_chart.html to display the Plotly bar chart. Ensure that the template extends base.html.
  4. Finally, modify the base template to include a link to the bar chart page. Therefore, you will need to modify the base.html file for this last step.
  • When you view the bar chart page, you should see a page that looks similar to the below.

Code Journal bar chart visualization page showing a horizontal bar chart that displays the number of entries per topic. The chart includes topics from the Code Journal with corresponding bars representing entry counts. Navigation breadcrumbs appear at the top linking back to the main Code Journal page. The page maintains a clean, professional design with a light background and dark text, providing a clear visual representation of learning progress across different CIS 240 topics.