Skillversitas

Forms and Formsets

The form is the validate the render: the Form the 3 (the field, the clean, the 12), the ModelForm (the model, the fields, the no the code), and the FormSet (the multi the 1 page, the inline the related). The widget is the HTML (the input, the select, the textarea), the clean is the rule (the field, the form, the no the DB), and the CSRF is the token (the {% csrf_token %}, the no the 403). This chapter covers the Form (the 12 the field), the widget (the override), the clean (the 2 the level), the ModelForm (the no the code), and the FormSet (the inline, the 1 page).

The Form: The 12 field

# the FORM (the 12, the field's the full):
from django import forms

class NoteForm(forms.Form):
    #   the 12 (the type's the full):
    title = forms.CharField(
        max_length=200,
        widget=forms.TextInput(
            attrs={"class": "input"}))
    body = forms.CharField(
        widget=forms.Textarea(
            attrs={"rows": 6}))
    #   the no: the clean, the
    #     the field, the no the
    #     the DB:
    def clean_title(self):
        t = self.cleaned_data["title"]
        return t.strip().title()
    #   the the 2: the clean (the
    #     the form, the field's
    #     the no, the 1 the
    #     the no):
    def clean(self):
        cd = self.cleaned_data
        if cd.get("title") == \
            cd.get("body"):
            self.add_error("body",
                "the no the title")
        return cd
#   the VALIDATE (the view):
# form = NoteForm(request.POST)
# if form.is_valid():
#     t = form.cleaned_data["title"]
#     the no: the DB, the
#       the save, the redirect
# else:
#     the no: the re-render (the
#       the error, the no the
#       the data the no)

The Form is the 12: the CharField (the max_length, the strip), the IntegerField (the min the max), the ChoiceField (the choices, the select), the BooleanField (the checkbox), the DateField (the input_formats), the FileField (the upload), the EmailField (the regex), the URLField, the SlugField, the DecimalField (the money), the ModelChoiceField (the QuerySet, the select), and the TypedChoiceField (the cast, the no the string) - the widget is the HTML: the TextInput (the input, the attrs the class), the Textarea (the rows), the Select (the options), and the RadioSelect (the 1 the 1 the row) - the clean is the 2 the level: the clean_title (the 1 the field, the strip the title), the clean (the form, the field the cross, the add_error the field), and the ValidationError is the no the raise (the form the error, the no the 500) - the ModelForm is the next: the no the code.

The ModelForm and the FormSet

# the MODELFORM (the no the code):
class NoteModelForm(forms.ModelForm):
    class Meta:
        model = Note
        fields = ["title", "body"]
        #   the the no: the exclude
        #     (the created, the views,
        #     the no the user's)
        widgets = {
            "body": forms.Textarea(
                attrs={"rows": 8}),
        }
#   the SAVE (the view):
# form = NoteModelForm(request.POST,
#     instance=note)   #   the
#                       #     the UpdateView
# if form.is_valid():
#     n = form.save()   #   the
#                       #     the model
#     the redirect (the
#       the no the 2)

# the FORMSET (the multi, the 1 page):
from django.forms import (
    formset_factory,
    inlineformset_factory)
NoteSet = formset_factory(
    NoteModelForm, extra=2)
#   the the no: the total_form_count,
#     the 1 (the no the no), the
#     no the 1 the form:
formset = NoteSet(request.POST)
if formset.is_valid():
    formset.save()   #   the the 1
                      #     the 1 (the
                      #     no the loop)
#   the INLINE (the related, the
#     the no the FK):
TagInline = inlineformset_factory(
    Note, Tag, fields=["name"],
    extra=3)
#   the 1 page: the note +
#     the 3 tag (the no the
#     the 2 page, the no
#     the no)

The ModelForm is the no the code: the Meta.model plus the fields (the 1 the list, the no the field the write), the exclude is the no (the created, the views, the user the no), and the widgets is the override (the Textarea the 8 the row) - the save is the model (the 1 the call, the instance the UpdateView, the commit=False is the no the save the set the author the 1 the no). The FormSet is the multi: the formset_factory plus the extra=2 (the 1 the blank), the is_valid is the all (the 1 the no, the no the save), and the save is the 1 (the no the loop) - the inlineformset_factory is the FK (the Note plus the 3 the Tag, the 1 page, the no the 2) - the rule is the one: the Form the 12, the widget the override, the clean the 2, the ModelForm the no the code, and the FormSet the inline - the next chapter's template is the render the extend the block the filter.

The Working Rules

  • The Form is the 12, the widget the override. The CharField the max_length, the ChoiceField the select, and the ModelChoiceField the QuerySet (the no the value the string) - the TextInput the attrs (the CSS the class), and the no the field (the required, the label, the error the 1).
  • The clean is the 2, the ValidationError the no. The clean_title the 1 field (the strip, the title), and the clean the cross (the add_error, the field the no) - the ValidationError the raise (the form the error, the no the 500), and the no the DB (the clean, the no the save).
  • The ModelForm is the no code, the commit=False the set. The Meta.model plus the fields (the 1 the list), the exclude the no (the created, the user), and the save the instance (the UpdateView) - the commit=False the author (the request.user, the 1 the no), and the error the no the data (the re-render, the POST the copy).
  • The FormSet is the multi, the inline the FK. The formset_factory the extra (the blank, the no the no), and the is_valid the all (the 1 the no, the no the save) - the save the 1 (the no the loop), and the inlineformset_factory the 1 page (the note plus the 3 the tag, the no the 2).

Summary

The form is the validate the render: the Form the 12 (the CharField, the IntegerField, the ChoiceField, the BooleanField, the DateField, the FileField, the EmailField, the URLField, the SlugField, the DecimalField, the ModelChoiceField, and the TypedChoiceField), the widget the override (the TextInput the attrs, the Textarea the rows, the Select the options), the clean the 2 (the 1 field the strip, the form the cross, the ValidationError the no the 500), the ModelForm the no code (the Meta.model the fields, the exclude the no, the commit=False the author, the instance the UpdateView), and the FormSet the multi (the formset_factory the extra, the is_valid the all, the save the 1, the inlineformset_factory the FK the 1 page). The CSRF is the no (the {% csrf_token %} the form, the no the 403) - the next chapter's template is the render: the {% extends %} the block, the {% for %} the loop, the filter the 20 (the date, the length, the truncate, the no the escape), and the {% include %} the partial.