Django Forms

Arguments

views.py:

def get_form_kwargs(self):
    kwargs = super().get_form_kwargs()
    kwargs.update({"contact": self._contact()})
    return kwargs

forms.py:

class InvoiceUpdateForm(RequiredFieldForm):
    def __init__(self, *args, **kwargs):
        self.contact = kwargs.pop("contact")
        super().__init__(*args, **kwargs)

Hidden

To hide a field:

if not settings.show_method:
    self.fields["methods"].widget = forms.HiddenInput()

Label

To change the label:

self.fields["category"].label = "Category"

Required

for name in ("assessor_comments", "result"):
   self.fields[name].required = True

Testing

assert HTTPStatus.FOUND == response.status_code, response.context["form"].errors