Underoos
Underoos

Reputation: 5190

How to render two form fields as one field in Django forms?

I'm working on Django application where I need to render a form differently.

The form contains multiple fields related to a person.

 1. Firstname
 2. Lastname
 3. Email
 4. Address
 5. City
 6. Country
 7. Phone
 8. Pincode

There are two flows in my application. In the first flow I can render all the form fields and save them when the user enters some data and submit.

But in the second flow, I need to display only three fields as below.

 1. Name - *Combination of Firstname and Lastname*
 2. Email
 3. Phone

The data entered in the name field should be split by empty space - and saved as Firstname and Lastname. And for other remaining mandatory fields in the form that are not rendered, I can fill empty values and save the model in Backend.

What is the best way to render the forms differently for each flow?

Python: 3.7.3
Django: 2.1.5

Upvotes: 0

Views: 759

Answers (1)

Haider Ghufran
Haider Ghufran

Reputation: 133

You can use Post / Get feature of request. For example: pseudo code

def DataView(request):
    if request.method == 'POST':
        form = DataForm(request.POST)
        if form.is_valid():
            form.save()
            return render(request, 'users/form1.html', {'form': form})
        else
            ValidationError(_('Please login-in first'), code='invalid')
    else: 
        #do your magic here. make a new form with limited fields or render this old form on another page where in html you can limit/modify the fields.
        form = DataForm1()
        return render(request, 'users/form1.html', {'form': form})

    

Upvotes: 1

Related Questions