python / beginner
Snippet
Creating a Simple Django Form
Forms handle user input and validation logic in Django. This approach allows you to define fields and their requirements cleanly.
snippet.py
python
1
2
3
4
5
6
from django import formsclass ContactForm(forms.Form):subject = forms.CharField(max_length=100)message = forms.CharField(widget=forms.Textarea)sender = forms.EmailField()
django
Breakdown
1
class ContactForm(forms.Form):
Defines a new form class by inheriting from the Django Form class.
2
subject = forms.CharField(max_length=100)
Creates a character field for short text with a maximum length.
3
widget=forms.Textarea
Changes the default input to a larger text area box.