Overview
The Proyecto system uses Django’s ModelForm framework to handle form rendering, validation, and data processing. All forms are defined inCTP/forms.py and inherit from forms.ModelForm, providing automatic form generation from Django models with customizable widgets and validation.
Location: CTP/forms.py
Form Classes
ProjectForm
ModelForm for creating and editing projects (Proyectos). Location:CTP/forms.py:27
Meta Configuration
The Django model this form is based on
List of model fields to include in the form
Fields
Widget:
TextInput with CSS class form-controlLabel: “nombre_proyecto”Description: Project name field. Must be unique per validation in viewProyectos.HTML Attributes:Widget:
Select with CSS class form-controlLabel: “lider”Description: Project leader selection. References the encargado model as a foreign key.HTML Attributes:Widget:
SelectMultiple with CSS classes form_control select2Label: “encargados”Description: Multiple team member selection. Uses Select2 JavaScript library for enhanced multi-select functionality.HTML Attributes:The
select2 class enables the Select2 jQuery plugin for searchable dropdown with multiple selections.Usage Example
TareasForm
ModelForm for creating and editing tasks (Tareas). Location:CTP/forms.py:17
Meta Configuration
The Django model this form is based on
List of model fields to include in the form
Fields
Widget:
TextInput with CSS class form-controlLabel: “nombre_tarea”Description: Task name field. Must be unique per validation in viewTareas.HTML Attributes:Widget:
Select with CSS class form-controlLabel: “lider” (note: label differs from field name)Description: Team member assigned to the task. References the encargado model as a foreign key.HTML Attributes:The label is set to “lider” while the field name is “encargados”, which may cause confusion. This appears to be a naming inconsistency in the original code.
Usage Example
trabajadoresForm
ModelForm for creating and editing team members (encargado). Location:CTP/forms.py:7
Meta Configuration
The Django model this form is based on
List of model fields to include in the form (single field)
Fields
Widget: Placeholder: “Ingresa el nombre” (Spanish: “Enter the name”)
TextInput with CSS class form-control and placeholderLabel: “nombres”Description: Team member name field. Must be unique per validation in viewtrabajadores.HTML Attributes:Usage Example
encargadoform
ModelForm for selecting team members (encargados) from projects. Location:CTP/forms.py:38
Meta Configuration
The Django model this form is based on (uses Proyectos model)
List of model fields to include in the form (single field)
Fields
Widget:
Select (single selection from many-to-many relationship)Label: “encargados”Description: Single team member selection from project’s assigned team members.HTML Attributes:Despite being a ManyToManyField in the model, this form uses a
Select widget (not SelectMultiple), limiting selection to a single team member.Usage Example
Form Architecture
Base Class
All forms inherit fromdjango.forms.ModelForm, which provides:
- Automatic form field generation from model fields
- Built-in validation based on model field constraints
- Easy integration with Django’s ORM
- Automatic form rendering in templates
Widget System
Forms use Django’s widget system to control HTML rendering:Renders as
<input type="text"> HTML elementCommon Usage: Text fields, names, titlesRenders as
<select> HTML dropdown elementCommon Usage: Foreign key relationships, single choice fieldsRenders as
<select multiple> HTML elementCommon Usage: Many-to-many relationships, multiple choice fieldsCSS Framework Integration
All forms use Bootstrap CSS classes for styling:The
form-control class is from Bootstrap and provides consistent styling across all form inputs. Some forms also use select2 for enhanced JavaScript functionality.Form Validation
Automatic Validation
ModelForms automatically validate based on model constraints:Custom Validation
Views implement additional validation logic: Duplicate Checking:Form Rendering
Template Usage
Forms can be rendered in templates using various methods: Full Form:JavaScript Integration
Forms integrate with JavaScript libraries:Enhanced multi-select dropdowns with search functionalityUsage: Applied to
ProjectForm.encargados field via CSS classDependencies
UserCreationForm is imported but not used in the current implementation. This may be for future authentication features.Related Resources
- Project Views - Views using ProjectForm
- Task Views - Views using TareasForm
- Team Views - Views using trabajadoresForm
- Proyectos Model
- Tareas Model
- encargado Model