How to create multipage forms with different models

83 views
Skip to first unread message

victor awakan

unread,
Mar 2, 2020, 7:38:44 AM3/2/20
to Django users
I am a building a small project in Django. I am able to create two or more forms in a template but now, I want to split the form to separate templates with each form belong to different models. How can I achieve this? Here is a link to dpaste with the multiple forms in a template I created: http://dpaste.com/1WX0RC8

All help and suggestions will be appreciated.

PS. I have tried it with formtools and it worked. I am trying to learn how to do the same thing with function based views. Thanks

Naveen Arora

unread,
Mar 3, 2020, 4:19:40 AM3/3/20
to Django users
Hi,

What problem you are facing ? A form can be sent to any template using its object and context dictionary. "Split the form to separate templates" ? This seems unusual, create different forms if you want to use more templates. 

Cheers :)
Naveen Arora

victor awakan

unread,
Mar 3, 2020, 4:28:05 AM3/3/20
to Django users
What I meant was I want one form to be saved in one model and the second form to be saved in another model. I did that using form-tools but I want to learn how to do it using function based views. Thanks.

Suraj Thapa FC

unread,
Mar 3, 2020, 4:45:01 AM3/3/20
to django...@googlegroups.com
There can be only one view for one page....
You have to implement both in same view 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0c158202-002d-484d-b0c1-332ca7e3c606%40googlegroups.com.

victor awakan

unread,
Mar 3, 2020, 4:49:09 AM3/3/20
to django...@googlegroups.com
@suraj Could you show by example? I have tried one way which I posted the dpaste link with my first post but it doesn’t work. I.e when I clicked submit I don’t get any new post in the template. Thanks 

Naveen Arora

unread,
Mar 4, 2020, 1:25:15 AM3/4/20
to Django users
Each form field has an id to be identified at back end. So just select the relevant field create a model object using the same and save it.
Similarly do for the other.

You can try for once share the code here if you are unsuccessfull, i will help you with the code part then.

Cheers

victor awakan

unread,
Mar 4, 2020, 1:32:55 AM3/4/20
to Django users

Thanks Naveen, I have try this code but when I clicked saved, the views redirect but no post was showing in the list view.

def new_rental(request, pk):
    rentalproperty = get_object_or_404(RentalProperty, pk=pk)
    user = UserModel.objects.first()
    if request.method == 'POST':
        form = NewRentalPropertyForm(request.POST, request.FILES)
        #contract_form = NewContractForm(request.POST, prefix = "contracts")
        if form.is_valid():
            print ("all validation passed")
            rentalproperty = form.save()
            #contract_form.cleaned_data["rentalproperty"] = rentalproperty
            #contract.rentalproperty = rentalproperty
            #contract = contract_form.save()
            return HttpResponseRedirect(reverse("rental:new_contract"))
        else:
            messages.error(request, "Error")
            
    else: 
        form = NewRentalPropertyForm()
        #contract_form = NewContractForm(prefix = "contracts")
    return render(request, 'rental/new_rental.html', {
        'rentalproperty': rentalproperty,
        'form': form,
        #'contract_form': contract_form,
        })

    
def new_contract(request, pk):
    rentalproperty = get_object_or_404(RentalProperty, pk=pk)
    if request.method == 'POST':
        form = NewContractForm(request.POST)
        if form.is_valid():
            contract = form.save(commit=False)
            contract.rentalproperty = rentalproperty
            contract.save()
            return HttpResponseRedirect(reverse("rental:home"))
        else:
            messages.error(request, "Error")
            
    else:
        form = NewContractForm()
    return render(request, 'rental/new_contract.html', {
        'rentalproperty': rentalproperty,
        'form': form,
        })
Reply all
Reply to author
Forward
0 new messages