django-forms
Skill177 starsChanged 10 months agoMITView on GitHub
Django form handling patterns including ModelForm, validation, clean methods, and HTMX form submission. Use when building forms, implementing validation, or handling form submission.
We split this skill into the rules it gives an agent
13 rules. About 0 are common, 6 are distinctive, and 7 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 10
Prefer ModelForm for model-backed forms
- Distinctive: line 11
Keep validation logic in forms, not views
- Distinctive: line 12
Always handle and display form errors
- Very specific: line 13
Use commit=False when you need to modify the instance before saving
- Distinctive: line 20
Use for: format checks, uniqueness, normalization
- Very specific: line 25
Use self.add_error(field, message) for field-specific errors
- Distinctive: line 26
Use for: password confirmation, conditional requirements
- Very specific: line 32
Use form.save(commit=False) to set additional fields (e.g., author)
- Very specific: line 36
Check request.headers.get("HX-Request") for HTMX requests
- Very specific: line 38
Use HX-Trigger header to notify other components
- Very specific: line 44
Use partial templates (_form.html) for HTMX responses
- Very specific: line 55
Use inlineformset_factory for related model collections
- Distinctive: line 64
Not displaying form errors to users
≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Django Forms
Philosophy
- Prefer ModelForm for model-backed forms
- Keep validation logic in forms, not views
- Always handle and display form errors
- Use
commit=Falsewhen you need to modify the instance before saving
Validation
Field-level (clean_<field>):
- Validate and transform a single field
- Return the cleaned value or raise
ValidationError - Use for: format checks, uniqueness, normalization
Cross-field (clean):
- Call
super().clean()first - Access multiple fields via
cleaned_data - Use
self.add_error(field, message)for field-specific errors - Use for: password confirmation, conditional requirements
View Integration
- Check
request.methodexplicitly - Instantiate form with
request.POSTfor POST, empty for GET - Use
form.save(commit=False)to set additional fields (e.g., author) - Return redirect on success, re-render with form on error
HTMX handling:
- Check
request.headers.get("HX-Request")for HTMX requests - Return partial template on success/error for HTMX
- Use
HX-Triggerheader to notify other components
Templates
- Display
form.non_field_errorsfor cross-field errors - Display
field.errorsfor each field - Use partial templates (
_form.html) for HTMX responses - Include loading indicator with
hx-indicator
Widgets
- Override in
Meta.widgetsdict - Set HTML attributes via
attrsparameter - Common:
Textarea(attrs={"rows": 5}),DateTimeInput(attrs={"type": "datetime-local"})
Formsets
- Use
inlineformset_factoryfor related model collections - Validate both form and formset:
form.is_valid() and formset.is_valid() - Pass
instancefor editing existing parent objects
Pitfalls
- Validating in views instead of forms
- Silently redirecting without checking
is_valid() - Forgetting
commit=Falsewhen setting related fields - Not displaying form errors to users
In this repository16 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- celery-patternsSkill
- code-qualitySkill
- django-extensionsSkill
- django-modelsSkill
- django-templatesSkill
- docs-syncSkill
- htmx-patternsSkill
- onboardSkill
- pr-reviewSkill
- pr-summarySkill
- pytest-django-patternsSkill
- skill-creatorSkill
- systematic-debuggingSkill
- ticketSkill
- worktree-commit-mergeSkill
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

