In-Class Activity 7: Django Forms:

Objective: Create a form and add pages that allow users to enter their own data.

Description: Build a Django form that handles GET and POST requests that allow users to add new topics which will save to the database.

  • Make a copy of your Activity6 folder and name the folder Activity7-DjangoForms. After the activity is completed, take two 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.

  1. Create your first 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_topic page. This pattern should send requests to the view function new_topic().
  3. Define a new view function, new_topic(), to handle GET and POST requests in the views.py file. In the file, make sure to import:
    • i. HttpResponseRedirect
    • ii. reverse
    • iii. The TopicForm model
  4. Create a new template called new_topic.html to display the form. Ensure that the template extends base.html.
  5. Finally, append the topics template (topics.html) to link the new_topic page on the topics page.

  • The first screen capture below shows the resulting form. Use the form to add an additional topic called, Dictionaries. Once the topic is added, you should have the following list of topics, displayed below in the second screen capture.

new_topic template: Browser window on localhost 127.0.0.1:8000/new_topic/ showing the Code Journal new topic form. At the top is a purple underlined navigation link reading Code Journal - Topics. Below it, a heading reads Add a new topic:. A single empty text input box appears, followed by a gray button labeled add topic. The page is a simple light gray Django template with large open space, and the overall tone is plain, neutral, and instructional.


topics template: Browser window on localhost showing the Code Journal Topics page after submitting the form, with a list of topics that now includes Dictionaries as a new item. The page layout is a minimal Django template with topic links in a vertical list and surrounding white or light gray background space. The screen communicates successful progress and has a practical, instructional tone.