Django Forms

Learn how to manage forms in Django - For beginners

This page explains how to manage and validate a form in Django Framework. We assume the audience has already a Django project up and running. For those that start from nothing, feel free to access the resources mentioned below and come back here once the content is understood:

Topics covered by this tutorial

  • ✅ Create a new Django app

  • ✅ Define a new route

  • ✅ Update the configuration to include the new application

  • ✅ Code a simple form

  • ✅ Integrate the Form into a real page

  • ✅ Update the controller to check the form is validated

The video presentation of this tutorial can be found on Github:

Django For Beginners - How to add a new form

Create a new Application

For this, we will use the startapp Django subcommand:

Add a simple view in forms - forms/views.py

Define the routing for our new app - forms/urls.py

Update settings to include the new app - config/settings.py

Update routing - config/urls.py

Once we restart the project, the new route /forms should be visible:

Django Forms - A Hello-World type route

Define a new form

Inside the newly created app, we need to create a forms file and define the new Form Class object.

Controller code - forms/views.py, the index() method

Update App configuration to include the new Templates folder, core/settings.py for AppSeed projects.

HTML page - defined in forms/templates folder:

Django Forms - Completed with user Data

If all fields are provided, on submit, we should see the Form is valid message.

Update the form with a new field ID (numeric type):

The page server to the users is updated automaticaly to include the new field

Django Forms - Integer Field Added

Thanks for reading! For more topics, feel free to contact Appseed.

Resources

Last updated

Was this helpful?