Reputation: 113
i m quite new with Django but i m stuck since a day on a problem that i can t solve.
I have to create forms with a lot of fields. The fields comes from different models and i want to save them all at once. I want to display it with a slick so the user experience will be better and not overwhelmed by the amout of fields to fill. The slick part is not a problem. My problem is to render multiple form separated in different div on the same page. In this i want to display the candidateDetails form not only the candidate one.
here my actual code for only one form that is working.
The forms :
class CandidateApplicationForm(ModelForm):
position = forms.ChoiceField(choices=POSITION, widget=forms.RadioSelect)
class Meta:
model = Candidate
fields = ['firstName',
'lastName',
'mailAddress',
'address',
'city',
'country',
'nationality',
'position',
]
widgets = {
'firstName': forms.TextInput(attrs={'class': 'input'}),
'lastName': forms.TextInput(attrs={'class': 'input'}),
'address': forms.TextInput(attrs={'class': 'input'}),
'city': forms.TextInput(attrs={'class': 'input'}),
'nationality': forms.TextInput(attrs={'class': 'input'}),
'Type of programme': forms.TextInput(attrs={'class': 'input'}),
}
class CandidateDetailsForm(ModelForm):
class Meta:
model = CandidatesDetail
fields = ['birthPlace',
'birthDate',
'nationality',
]
The Views :
class RegisterCandidate:
def __init__(self):
self.method = None
def candidateapply(request):
apply = candidateregistrationform.CandidateApplicationForm()
if request.method == 'POST':
form_candidate = candidateregistrationform.CandidateApplicationForm(request.POST)
if form_candidate.is_valid():
candidate = form_candidate.save(commit=False)
candidate.save()
else:
apply = candidateregistrationform.CandidateApplicationForm()
return render(request,
'candidateapplication.html',
{'form': apply})
def candidatedetails(request):
apply = candidateregistrationform.CandidateDetailsForm()
if request.method == 'POST':
form_candidatedetails = candidateregistrationform.CandidateDetailsForm(request.POST)
if form_candidatedetails.is_valid():
candidate = form_candidatedetails.save(commit=False)
candidate.save()
else:
apply = candidateregistrationform.CandidateApplicationForm()
return render(request,
'candidateapplication.html',
{'form': apply})
And the HTML :
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% endfor %}
Thanks for your help.
Upvotes: 1
Views: 74
Reputation: 237
yep, as dirkgroten said, create a single view function with two forms in it:
def candidatedetails(request):
candidate_application_form = candidateregistrationform.CandidateApplicationForm()
candidate_details_form = candidateregistrationform.CandidateDetailsForm()
...
return render(request,
'candidateapplication.html',
{'candidate_application_form': candidate_application_form,
'candidate_details_form', candidate_details_form})
now you'll be able to render these forms or specific fields
{{ candidate_application_form }}
Upvotes: 1