StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 10 · Forms

Contact Form Basics

easyDjango FormsInput Validation100%2 of 2 solved

Problem statement

The contact page accepts enquiries, but nothing checks them. ContactForm in app/forms.py declares no fields, so an empty submission looks exactly as valid as a real one and blanks and garbage reach the database. The contact view in app/views.py has no POST handling yet. Give the form its fields, then make the view bind the POST data to the form, gate on is_valid() and store a message only from form.cleaned_data.

Requirements

Implement:

  • the fields of ContactForm in app/forms.py
  • the POST side of the contact view in app/views.py

The following files are already implemented and provided: app/models.py, app/urls.py and the template. ContactMessage holds name, email, message and an automatic created_at. The contact view at /contact/ already renders the empty form for a GET.

1. The three fields

  • ContactForm declares name, email and message, and all three appear in cleaned_data after a valid submission.
  • name holds free text.
  • message holds free text, entered in a multi-line box rather than a single-line input.
  • email holds an email address, and a value that is not one is a validation error under the email key of form.errors.

2. Required by default

  • All three are required. A submission missing any one of them is invalid, and the error lands under that field's own name in form.errors - name, email or message - never on the form as a whole.

3. Cleaned values

  • A valid submission exposes all three values in cleaned_data with surrounding whitespace already stripped, so a submitted Ada Lovelace reaches the view and the database as Ada Lovelace.
  • A key the form never declared is not part of the submission, so a body carrying an extra field is still valid.

4. The view handles a POST

  • POST /contact/ binds ContactForm to request.POST and gates on is_valid().
  • A valid submission creates one ContactMessage from form.cleaned_data["name"], form.cleaned_data["email"] and form.cleaned_data["message"], then redirects to the route named contact.
  • An invalid submission re-renders app/contact.html with the bound form and status 400, and stores nothing.

Notes

  • Only app/forms.py and app/views.py change.
  • Declaring a field is already enough to require it: required=True is the default, and each field class carries its own type conversion and its own validation.
  • Read request.POST only to bind the form; every stored value comes from form.cleaned_data.
  • Do not add length limits, format rules or any validation beyond the field types and the required rule.

More in Forms

See the module
Custom Field RulesAdd per-field validation with a clean_<field> method and a reusable validator shared across two forms.100%2 of 2 solvedeasyCross-Field Password CheckCompare two password fields in the form's clean() method and render field and non-field errors in the template.100%2 of 2 solvedeasyEvent Registration FormBuild an event registration form end to end: typed required fields, a reusable validator, per-field and cross-field rules, error rendering and the view flow.100%2 of 2 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.