How to create a view with more than one form in Django

Asked

Viewed 183 times

0

I have model, form and template for units, and in this same template I need to add a region form (these are exams). How can I add this form in the view so it can be edited in the drive template?

py views.

class CreateUnitView(CreateView):
form_class = UnitForm
template_name = 'units/unit_form.html'
success_url = 'list' 

def get_context_data(self, **kwargs):
    pdb.set_trace()
    context = super(CreateUnitView, self).get_context_data(**kwargs)
    if self.request.POST:
        pdb.set_trace()
        context['forms'] = UnitForm(self.request.POST)
    else:
        context['forms'] = UnitForm()
    return context

def form_valid(self, form):
    context = self.get_context_data()
    forms = context['forms']
    if forms.is_valid(): 
        self.object = form.save()
        forms.instance = self.object
        forms.save()
        return redirect('/units/list')
    else:
        return self.render_to_response(self.get_context_data(form=form))

def get_success_url(self):
    return self.success_url

template:

<h4 class="ui divinding header">Preços por região</h4>
 <table id="unit-table" class="ui small table">
            <thead>
            <tr>
                <th>Região</th>
                <th>Imagens</th>
                <th>Fixa: 1a região</th>
                <th>Demais regiões</th>
                <th>Móvel: 1a região</th>
                <th>Demais regiões</th>
            </tr>
            </thead>
            <tbody>
            {% for region in object_list %}
                <tr>
                    <td>
                            {{ region.name }}
                        </a>
                    </td>
                    <td>
                            {{ region.num_images }}
                    </td>
                    <td>
                        {{ region.price_1fix }}
                    </td>
                    <td>
                        {{ region.price_2fix }}
                    </td>
                    <td>
                        {{ region.price_1 }}
                    </td>
                    <td>
                        {{ region.price_2 }}
                    </td>
                </tr>
            {% endfor %}
            </tbody>
 </table>

The fields of this form will be shown in this table.

1 answer

1


Good morning the/,

You can render multiple Forms used the context_data method, thus:

def get_context_data(self, **kwargs):
    kwargs['form1'] = MeuForm1()
    kwargs['form2'] = MeuForm2()
    kwargs['form3'] = MeuForm3()
    return super().get_context_data(**kwargs)

In your template, you can render the Forms like this:

{{ form1.as_p }}
{{ form2.as_p }}
{{ form3.as_p }}

And to process the result, you can override form_valid, like this:

def form_valid(self, form):
    form1 = MeuForm1(self.request.POST or None)
    form2 = MeuForm2(self.request.POST or None)
    form3 = MeuForm3(self.request.POST or None)

Then, just check if you have valid data and save each one:

if form1.is_valid():
    form1.save()
if form2.is_valid():
    form2.save()
if form3.is_valid():
    form3.save()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.