Skip to content
Learn Netverks

Lesson

Step 26/36 72% through track

validation-errors

Validation and errors

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Validation and errors: the syntax, APIs, and habits you need before advancing in Django.

Teams ship Validation and errors on every Django codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Validation and errors in contexts like: SaaS dashboards, CMS-style products, internal tools, and APIs paired with React or mobile clients.

Write Python 3 in the editor and click Run on server—the dev runner executes your script; Django framework lessons also use local startproject for full MVT (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Validation happens at multiple layers: HTML5 client hints, Django form fields, model clean() methods, and database constraints. Never trust the client alone.

Form-level validation

def clean_title(self):
    title = self.cleaned_data["title"]
    if "spam" in title.lower():
        raise ValidationError("Title not allowed")
    return title

Displaying errors

{{ form.non_field_errors }} for form-wide issues; {{ form.title.errors }} per field. Re-render the form with errors on failed POST—preserve user input.

Important interview questions and answers

  1. Q: clean vs clean_fieldname?
    A: Field cleaners run per field; clean() validates cross-field rules (e.g. password match).
  2. Q: Model validation on save?
    A: Call full_clean() before save in custom code—ModelForm does this automatically.
  3. Q: ValidationError in models?
    A: Raise in clean() methods—surfaces in forms and admin.

Self-check

  1. Why re-render the form after validation fails?
  2. Where add cross-field validation?

Tip: Re-display bound forms on validation failure so users do not retype everything—pass the same form instance back to the template.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Field error display?
  • non_field_errors?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump