0

I am using django class based view to create two forms (Thread, Message).

views.py

class ThreadForm(FormView):
    template_name = 'thread.html'
    form_class = ThreadModelForm
    success_url = '/success'

    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
        return super(ThreadForm, self).form_valid(form)

class MessageForm(FormView):
    template_name = 'thread.html'
    form_class = MessageModelForm
    success_url = '/success'

    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
        return super(MessageForm, self).form_valid(form)

Both are the rendering the same html file thread.html. Could anyone tell me how can I render both the forms in the same template?

thread.html

{{ form.as_p }}

UPDATE: I am using the view below but it is not working:

class MessageForm(FormView):
    template_name = 'thread.html'
    success_url = '/success'

    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
        return super(MessageForm, self).form_valid(form)

    def get_context_data(self, **kwargs):
        context = super(MessageForm, self).get_context_data(**kwargs)
        context['second_form'] = MessageModelForm
        return context
python
  • 4,403
  • 13
  • 56
  • 103
  • 1
    You may get help from below link [Same problem You have but he has done in a different way](http://stackoverflow.com/questions/18122096/django-class-based-views-updateview-with-two-model-forms-one-submit) – Hardik Patel Dec 09 '15 at 11:18

2 Answers2

2

use get_context_data method for this.

def get_context_data(self, **kwargs):
    context = super(FormViewName, self).get_context_data(**kwargs)
    context['second_form'] = SecondForm
    return context

Then in your template you can use

{{ second_form.as_p }}

Also in your form_valid method you've to check for second_form validity as well.

Tarun Behal
  • 908
  • 6
  • 11
  • Hey it is working now! Some issue with `urls.py` file :) and I don't need two class for it. – python Dec 09 '15 at 11:18
  • Could you tell me how to validate `second_form` here? I am using a single submit button from my template and it is only passing me the first form data. – python Dec 13 '15 at 12:58
1

I do it like this...

class SolicitudUpdate(UpdateView):
    model = Solicitud
    second_model = Persona
    template_name = 'adopcion/solicitud_form.html'
    form_class = SolicitudForm
    second_form_class = PersonaForm
    success_url = reverse_lazy('adopcion:solicitud_listar')


    def get_context_data(self, **kwargs):
        context = super(SolicitudUpdate, self).get_context_data(**kwargs)
        pk = self.kwargs.get('pk', 0)
        solicitud = self.model.objects.get(id=pk)
        persona = self.second_model.objects.get(id=solicitud.persona_id)
        if 'form' not in context:
            context['form'] = self.form_class()
        if 'form2' not in context:
            context['form2'] = self.second_form_class(instance=persona)
        context['id'] = pk
        return context
Angie Alejo
  • 743
  • 9
  • 15