I have been looking long and far for how to have 2 unique forms displayed on one page using the newer Django class based views method.
Can anybody reference anything? Or provide a basic example. Google is not being my "friend" for this.
The key is that you don't even have to use one of the FormView
subclasses to process forms. You just have to add the machinery for processing the form manually. In the case where you do use a FormView
subclass, it will only process 1 and only 1 form. So if you need two forms, you just have to handle the second one manually. I'm using DetailView
as a base class just to show that you don't even have to inherit from a 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With