Hey,
For some reason, my CreateView throws an error upon form_invalid (but does save correctly when the form is valid).
For instance:
class ProduitCreateView(LoginRequiredMixin, CreateView):
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
fourprod_formset = FourProdFormSet(self.request.POST)
cases_formset = CaseFormSet(self.request.POST)
form_val = form.is_valid()
fourprod_val = False
case_val = cases_formset.is_valid()
if (form_val and fourprod_val and case_val):
return self.form_valid(form, fourprod_formset, cases_formset)
else:
return self.form_invalid(form, fourprod_formset, cases_formset)
def form_invalid(self, form, formset_fourprod, formset_case):
return self.render_to_response(self.get_context_data(form=form, formset_fourprod=formset_fourprod, formset_case=formset_case))
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs) # throw error here
if self.request.POST:
ctx["form"] = ProduitForm(self.request.POST)
ctx["formset_fourprod"] = FourProdFormSet(self.request.POST)
ctx["formset_case"] = CaseFormSet(self.request.POST)
else:
ctx["form"] = ProduitForm()
ctx["formset_fourprod"] = FourProdFormSet()
ctx["formset_case"] = CaseFormSet()
return ctx
But I’m creating the object - so no, my view doesn’t have a object already, since it hasn’t saved as it was invalid? THe call to super().get_context_data(**kwargs) fails.
EDIT: the traceback