Wednesday, July 29, 2020

reg: error after I update my user profile page successfully

Hello All, 

I have been facing a very notorious issue. I say notorious because, I do not see any issue with my back-end functionality.

I have developed a method to update the default User model field(email), and other extended User model(custom fields like country, state, phone etc).

Below is the method:

@login_required(login_url="/login/")
def editUserProfile(request):
if request.method == "POST":
form = UserProfileUpdateForm(request.POST, instance=request.user) # default user profile update
obj = UserProfile.objects.get(user__id=request.user.id)
form1 = UserProfileForm(request.POST or None, instance=obj)

if form.is_valid() and form1.is_valid():
obj.Photo = form1.cleaned_data['Photo']
obj.dob = form1.cleaned_data['dob']
obj.country = form1.cleaned_data['country']
obj.State = form1.cleaned_data['State']
obj.District = form1.cleaned_data['District']
obj.phone = form1.cleaned_data['phone']
form.save()
form1.save()
messages.success(request, f'updated successfully')
return redirect('/profile1')
else:
messages.error(request, f'Please correct the error below.')
else:
form = UserProfileUpdateForm(instance=request.user)
form1 = UserProfileUpdateForm(instance=request.user)
return render(request, "authenticate\\editProfilePage.html", {'form': form, 'form1': form1})
The surprising part is that I am able to perform the user profile update successfully. 

So, I have issues as below:

In the first glance I do not see any issue immediately as my purpose of updating the profile is successful. However, to test, after I update a user profile I logout the user which redirects me to login page, and there I see error "Please correct the error below." which is coming from the "else" part of the update method. I also see a message "updated successfully" on the same login screen which is coming from the "if" part of the update method (screenshot attached -- update_error3).

So, I have below observations:

My update method "editUserProfile" is somehow calling the inner most "if - else" together.
I think the issue lies in the HTML page. I could say this because when I click on the "update" button from the profile page, I see "email" field appearing twice on the screen and the "update" button to insert the data to database(screenshot attached -- update_error1).
Now, I could see the rest of the fields only after I click on the "update" button further(screenshot attached -- update_error2). Furthermore, this is when I enter all my details and when I click on the "update" button, the data get saved successfully in the database and redirected to the profile page with the new data.
Finally, I think the issue is something related to the HTML code for the appearance of the "email" and other fields. May be I am wrong. 

I am also not able to understand the cause of the error " Please correct the error below"


{% load static %}
{% block content %}


<h2 class="text-center">Edit Profile</h2>
<form method="POST" action="{% url 'editUserProfile' %}">
{% csrf_token %}

{% if form.errors and form1.errors %}
{% if messages %}
<ul class="messages ">
{% for message in messages %}
<ol class="breadcrumb ">
<li {% if message.tags %} class="{{ message.tags }} " {% endif %}><strong>{{ message }} {{form.errors}}</stron </li>
</ol>
{% endfor %}
</ul>
{% endif %}
</div>
{% endif %}
{{ form.as_p }}
{{ form1.as_p }}
<!-- {{ form1.dob }}-->
<!-- {{ form1.country }}-->
<!-- {{ form1.State }}-->
<!-- {{ form1.District }}-->
<!-- {{ form1.phone }}-->

<input type="submit" value="update" class="btn btn-secondry">
</form>

<br/><br/>

{% endblock %}
I have tried many things, but none of them working. Kindly help me debug the issue.
Do let me know if more information is needed.

Thanks

No comments:

Post a Comment