-1
What I want to do is create 2 Forms simultaneously, on just one page 'index.html'. One would just be a normal form that will send a message to contact, and the other is a record that I need to save to the database. So far I have been able to create only using a form, this way.
class IndexView(FormView):
template_name = 'index.html'
form_class = ContatoForm
success_url = reverse_lazy('index')
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['servicos'] = Servico.objects.order_by('?').all()
context['funcionarios'] = Funcionario.objects.order_by('?').all()
context['portfolios'] = Portfolio.objects.order_by('?').all()
context['testimonials'] = Testimonial.objects.order_by('?').all()
return context
def form_valid(self, form, *args, **kwargs):
form.send_mail()
messages.success(self.request, 'E-mail enviado com sucesso')
return super(IndexView, self).form_valid(form, *args, **kwargs)
def form_invalid(self, form, *args, **kwargs):
messages.error(self.request, 'Erro ao enviar e-mail')
return super(IndexView, self).form_invalid(form, *args, **kwargs)
Now I needed to add another form, which is this
class RegistroModelForm(forms.ModelForm):
class Meta:
model = Registro
fields = ['nome', 'email', 'phone', 'area']
I would need to do a filtering also to know which Forms are going pro form_valid to be able to do an action or not, because they are different and independent forms that are on the same page. While Registromodelform is to save in the database, Contactoform is to send a message to me, so how would you filter? I also have another problem, the template is already ready and mounting in html, in case I would only reuse the fields.
– Renzo Prats
Hello to your other question, I usually do not use Formview class based, use Createview or Updateview. I edited my answer from your example with multiple Rfms. You’ll have to do something like this.
– Ernesto Casanova