Finding Django Class-based Views and Having Multiple Forms Using One-Page Examples

For a long time I looked at how to have two unique forms displayed on the same page using the new presentation method based on the Django class.

Can anyone refer to anything? Or provide a basic example. Google is not my "friend" for this.

+8
django class forms
source share
1 answer

The key is that you don’t even need to use one of the subclasses of FormView to handle forms. You just need to add the equipment for processing the form manually. In case you use a subclass of FormView , it processes only 1 and only 1 form. Therefore, if you need two forms, you just need to process the second manually. I use DetailView as a base class to show that you don't even need to inherit from the FormView type.

 class ManualFormView(DetailView): def get(self, request, *args, **kwargs): self.other_form = MyOtherForm() return super(ManualFormView, self).get(request, *args, **kwargs) def post(self, request, *args, **kwargs): self.other_form = MyOtherForm(request.POST) if self.other_form.is_valid(): self.other_form.save() # or whatever return HttpResponseRedirect('/some/other/view/') else: return super(ManualFormView, self).post(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super(ManualFormView, self).get_context_data(**kwargs) context['other_form'] = self.other_form return context 
+8
source share

All Articles