Solution details

As you must have noticed, you can solve this using the solution given in the dynamic form generation pattern. You just need to pass request.user or any of their characteristics as a keyword argument to the form. I would recommend the latter to minimize the coupling between the view and the form.

As in the previous example, we need to show an additional checkbox to the user. However, this will be shown only if the user is a member of the "VIP" group.

Let's take a look at how the GenericFormView derived view passes this information to the form:

class GenericFormView(generic.FormView): template_name = 'cbv-form.html' form_class = PersonDetailsForm success_url = reverse_lazy("home") def get_form_kwargs(self): kwargs = super().get_form_kwargs() ...

Get Django Design Patterns and Best Practices - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.