Django: FormView, get_context_data invalidate form validation and discards inputs

338 Views Asked by At

I'd like my FormView to conduct validation (showing which fields contains an error) as well as re-use already made input if form is invalid.

At the moment, if form is invalid:

  1. Error is not shown on form / form field (the red rectangle around, with error text below)
  2. Form is reloaded and made inputs are discarded (form re-loads as new).

It seems that get_context_data invalidate default behaviour of class based FormView

How do you validate several forms within a context, as well as, how do you re-load already attempted form data within such context?

class ProductFormView(FormView):
    template_name = 'product/product_create.html'
    model = Product
    success_message = f'Product created successfully'
    success_url = '/product/list'

    def get_context_data(self, **kwargs):
        context = super(ProductFormView, self).get_context_data(**kwargs)
        context['form_1'] = CommonFieldsForm(instance=self.model())
        context['form_2'] = PriceForm(instance=self.model())
        return context

    def form_invalid(self, form):
        response = super().form_invalid(form)
        return response

    def form_valid(self, form):
        response = super().form_valid(form)
        return response
0

There are 0 best solutions below