#form View
@login_required(login_url='login')
def profileSetting(request):
form = PasswordChangeForm(request.user)
userupdateform = UserUpdate()
if request.method == 'POST':
form = PasswordChangeForm(request.user,request.POST)
userupdateform = UserUpdate(request.POST,request.FILES,instance = request.user)
if len(str(request.POST['old_password'])) > 1:
if form.is_valid():
form.save()
update_session_auth_hash(request,form.user)
else:
if userupdateform.is_valid():
userupdateform.save()
return render(request,'userDash/profile_setting.html',{'form':form,'userform':userupdateform})
#forms.py
class UserUpdate(forms.ModelForm):
class Meta:
model = User
fields = ['name','email','image']
def clean_name(self):
name = self.cleaned_data.get('name')
if all(char.isalpha() or char.isspace() for char in name):
return name
raise forms.ValidationError('Only characters are allowed in the name.')
class UpdatePassword(PasswordChangeForm):
pass
When I submit the user update form, the password change validation pops up, and on the other side both forms work fine to change and update values.
In your view you are checking for both forms to be valid i.e.
if userupdateform.is_valid():
and if form.is_valid():
this line checks your both form i.e UserUpdate
and PasswordChangeForm
. That’s the reason you are getting validation error even when you submit the user update form.
Thank you @addwebsolution for your response, But I think I’m checking forms based on a condition. If the user types the old password in the PasswordChangeForm, its validation is activated; otherwise, I check the validations of the other form.
Can you share templates how you are rendering your forms and what is the error validation you are getting. Also the screenshot of the rendered page of those forms.
and after submiting the user detail this happens:
and if I fill up the both forms it’s Done, no validation errors comes up and data successfully saved
Here is template page:
<form method='post' enctype="multipart/form-data">
{% csrf_token %}
<div class="left-container">
<div class="p-container">
<p class="p-1">Profile Setting</p>
<p class="p-2">Public Profile</p>
</div>
<div class="icon-container">
<div id="profile-icon">
<div>
<img id='user-img' src="{{request.user.image.url}}" alt="">
</div>
</div>
<div class="profile-btn">
<input style='display:none'
type="file"
id="image-input"
accept="image/*"
name='image'
/>
<label id="file-input-label" class="image-label" onclick="changeProfile()" for="image-input">Change Profile</label>
<a href="{% url "delete-img" %}" class="delete-btn">
Delete Picture
</a>
</div>
</div>
<div class="input-container">
Name
<div class="name-input">
<img src="{% static "images/Group 18.png" %}" alt="" />
<input type="text" placeholder="Name" name='name' value="{{request.user.name}}" />
</div>
{% if userform.name.errors %}
{% for error in userform.name.errors %}
<p class='c-error'>{{error}}</p>
{% endfor %}
{% endif %}
Email
<div class="email-input">
<img src="{% static "images/mail email e-mail letter.png" %}" alt="" />
<input type="email" placeholder="Email" name='email' value="{{request.user.email}}" />
</div>
{% if userform.email.errors %}
{% for error in userform.email.errors %}
<p class='c-error'>{{error}}</p>
{% endfor %}
{% endif %}
</div>
</div>
</div>
<div class="col-sm-6">
<div class="right-container">
<div class="p2-container">
<p class="psw-heading">Change Password</p>
<p class="psw-p">
Follow the folowing pattern in order to change the Password
</p>
</div>
<div class="input-container">
Current Passowrd
<div class="password-input">
<img src="{% static "images/Group 19.png" %}" alt="" />
<input type="password" placeholder="Password" name='old_password' />
</div>
{% if form.old_password.errors %}
{% for error in form.old_password.errors %}
<p class='c-error'>{{error}}</p>
{% endfor %}
{% endif %}
New Password
<div class="password-input">
<img src="{% static "images/Group 20.png" %}" alt="" />
<input type="password" placeholder="New Password" name='new_password1' />
</div>
{% if form.new_password1.errors %}
{% for error in form.new_password1.errors %}
<p class='c-error'>{{error}}</p>
{% endfor %}
{% endif %}
Confirm New Password
<div class="password-input">
<img src="{% static "images/Group 20.png" %}" alt="" />
<input type="password" placeholder="Confirm Password" name='new_password2' />
</div>
{% if form.new_password2.errors %}
{% for error in form.new_password2.errors %}
<p class='c-error'>{{error}}</p>
{% endfor %}
{% endif %}
{% if messages %}
{% for message in messages %}
<p class='c-error'>{{message}}</p>
{% endfor %}
{% endif %}
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="btn-container">
<div class="save-btn">
<button type='submit' class="save-changes">Save Changes</button>
</div>
</div>
</div>
</div>
</form>
Okay, you can do something like this
old_password = request.POST.get("old_password", None)
# if len(str(request.POST['old_password'])) > 1:
if old_password:
if form.is_valid():
form.save()
update_session_auth_hash(request,form.user)
Print len(str(request.POST['old_password']))
this line and tell me what you will get by it.
When I type nothing it’s 0 and when i type some values it gives me the length of the value:

both statements are same I think so.
Okay, try one more thing…
if request.method == 'POST':
if len(str(request.POST['old_password'])) > 1:
form = PasswordChangeForm(request.user,request.POST)
if form.is_valid():
form.save()
update_session_auth_hash(request,form.user)
else:
userupdateform = UserUpdate(request.POST,request.FILES,instance = request.user)
if userupdateform.is_valid():
userupdateform.save()
I’ve just moved your particular forms within their respective conditions.
1 Like
Thank you so much @addwebsolution 