C
C
Cyber_bober2016-03-12 02:13:23
Django
Cyber_bober, 2016-03-12 02:13:23

How to handle multiple forms on one page?

Hello, I want to group forms on one page in tabs.
For example, here are the forms

class FarmEditContact(forms.ModelForm):
    class Meta:
      model = Ranch
      fields = ('address','coord','phone1','phone2','worktime', 'vk', 'fb','insta','ok')

class FarmEditOffers(forms.ModelForm):
    class Meta:
      model = Ranch
      fields = ('offer1', 'price1', 'offer2', 'price2', 'offer3', 'price3')

Here is the handler
def farm_edit(request, id):
    farm = get_object_or_404(Ranch, id=id)
    if request.method == "POST":
        form1 = FarmEditMain(request.POST, instance=farm)
        if form.is_valid():
            farm = form.save(commit=False)
            farm.owner = request.user
            farm.published_date = timezone.now()
            farm.save()
            return redirect('ranch.views.own_farm_detail', id=farm.id)
    else:
        form = FarmEditForm(instance=farm)
    return render(request, 'farms/farm_edit.html', {'form': form}

it processes only one form, how to make processing any form in it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2016-03-12
@Cyber_bober

Either this https://docs.djangoproject.com/ja/1.9/ref/forms/ap...
Or you can initialize both forms directly in the code (because your field sets do not match, and required=False does not, then there should be no ambiguities) and check for validity first the first, then the second.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question