Normally when updating, i use (request, id) and loop through the list of items in template so that i can get a particular item i want to update by id. But this case is different. Am having a single data and i want after a user save his data, he can update it at anytime. this is confusing to me because i have to use the same form for same user to update his information without creating another form. this is how i thought will work in my view below. please i need help
views.py
if request.method == "POST":
form = usersForm(request.POST, request.FILES)
if form.is_valid():
form.instance.user =request.user
first_name =form.cleaned_data.get('first_name')
second_name =form.cleaned_data.get('second_name')
marital_status =form.cleaned_data.get('marital_status')
dob =form.cleaned_data.get('dob')
job =form.cleaned_data.get('job')
academy =form.cleaned_data.get('academy')
course_of_study =form.cleaned_data.get('course_of_study')
contact_address =form.cleaned_data.get('contact_address')
gender =form.cleaned_data.get('gender')
passport =form.cleaned_data.get('passport')
phone = form.cleaned_data.get('phone')
form.save()
user = request.user
applicant = Personalinfo.objects.get(user=user)
applicant.first_name = first_name
applicant.second_name = second_name
applicant.marital_status=marital_status
applicant.dob=dob
applicant.job=job
applicant.academy =academy
applicant.course_of_study = course_of_study
applicant.contact_address=contact_address
applicant.gender=gender
applicant.passport=passport
applicant.phone=phone
applicant.save()
messages.success(request, 'User data saved successful! Click next to proceed')
return redirect('user_info')
else:
messages.error(request, 'Error! Check your data')
return redirect('user_info')
else:
form = usersForm()
return render(request, 'portal/home/user_info.html', {"form": form})
form.py
class usersForm(forms.ModelForm):
first_name = forms.CharField(
widget=forms.TextInput(
attrs={
"placeholder": "First Name",
"class": "form-control",
"value":"First name"
}
))
second_name = forms.CharField(
widget=forms.TextInput(
attrs={
"placeholder": "Second Name",
"class": "form-control"
}
))
dob = forms.DateField(
widget=forms.DateInput(
attrs={
"placeholder": "Date of Birth",
"class": "form-control",
"type":"date"
}
))
widgets= {
'job': forms.Select(choices=JOB_CHOICES,
attrs={
"placeholder": "Available Job",
"class": "form-control"
})
}
course_of_study = forms.CharField(
widget=forms.TextInput(
attrs={
"placeholder": "course of study",
"class": "form-control"
}
))
contact_address = forms.CharField(
widget=forms.TextInput(
attrs={
"placeholder": "Address",
"class": "form-control"
}
))
widgets= {
'gender': forms.Select(choices=GENDER_CHOICES,
attrs={
"placeholder": "Marital Status",
"class": "form-control"
})
}
widgets= {
'marital_status': forms.Select(choices=MARITAL_CHOICES,
attrs={
"placeholder": "Marital Status",
"class": "form-control"
})
}
phone = forms.DecimalField(
widget=forms.NumberInput(
attrs={
"placeholder": "Phone",
"class": "form-control"
}
))
passport = forms.ImageField(
)
class Meta:
model =Personalinfo
fields=('first_name', 'second_name', 'marital_status', 'dob', 'job', 'course_of_study', 'contact_address',
'gender', 'passport')
this is the template
<form role="form" method="post" enctype="multipart/form-data" action="{% url 'user_info' %}">
{% csrf_token %}
<div class="row">
<div class="col-md-6">
<div class="input-group input-group-outline mb-3">
{{ form.first_name}}
</div>
<span class="text-danger">{{ form.first_name.errors }}</span>
</div>
<div class="col-md-6">
<div class="input-group input-group-outline mb-3">
{{ form.second_name }}
</div>
<span class="text-danger">{{ form.second.errors }}</span>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="input-group input-group-outline mb-3">
{{ form.dob}}
</div>
<span class="text-danger">{{ form.dob.errors }}</span>
</div>
<div class="col-md-6">
<div class="input-group input-group-outline mb-3">
{{ form.job }}
</div>
<span class="text-danger">{{ form.job.errors }}</span>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="input-group input-group-outline mb-3">
{{ form.gender}}
</div>
<span class="text-danger">{{ form.gender.errors }}</span>
</div>
<div class="col-md-6">
<div class="input-group input-group-outline mb-3">
{{ form.marital_status }}
</div>
<span class="text-danger">{{ form.marital_status.errors }}</span>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="input-group input-group-outline mb-3">
{{ form.contact_address }}
</div>
<span class="text-danger">{{ form.contact.errors }}</span>
</div>
<div class="col-md-6">
<div class="input-group input-group-outline mb-3">
{{ form.course_of_study }}
</div>
<span class="text-danger">{{ form.course_of_study.errors }}</span>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="input-group input-group-outline mb-3">
{{ form.phone }}
</div>
<span class="text-danger">{{ form.phone.errors }}</span>
</div>
<div class="col-md-4">
<div class="input-group input-group-outline mb-3">
{{ form.passport }}
</div>
<span class="text-danger">{{ form.passport.errors }}</span>
</div>
</div>
<div class="form-check form-check-info text-start ps-0">
Please dont forget to save your data before moving to next process <a href="javascript:;" class="text-dark font-weight-bolder">Review your data before you save</a>
</div>
<div class="row">
<div class="col-md-4">
<div class="text-center">
<button type="submit" name="register" class="text-white btn btn-lg bg-dark btn-lg w-100 mt-4 mb-0">
Save data
</button>
</div>
</div>
<div class="col-md-4">
<div class="text-center">
<a class="btn btn-dark text-decoration-none text-white btn btn-lg bg-dark btn-lg w-100 mt-4 mb-0" href="{% url 'academy' %}">Next --></a>
</div>
</div>
</div>
</form>