I have a profile page where I am fetching data from two models. One from the
default User model and another one is custom UserProfile.
However, The data from the custom model is getting populated, but not the User
model.Below are the two functions responsible for the whole procedure.
def userprofileview(request): # Authenticated user filling the form to
complete the registration if request.method == 'POST': form =
UserProfileForm(request.POST, request.FILES) if form.is_valid():
pr = UserProfile(User)
pr.user = User.objects.get(id=request.user.id)
pr.first_name = form.cleaned_data['first_name'] pr.last_name =
form.cleaned_data['last_name'] pr.email = form.cleaned_data['email']
pr.dob = form.cleaned_data['dob'] pr.country =
form.cleaned_data['country'] pr.State = form.cleaned_data['State']
pr.District = form.cleaned_data['District'] pr.phone =
form.cleaned_data['phone'] pr.save()
messages.success(request, f'Profile has been updated successfully')
return redirect('/profile') else:
messages.error(request, AssertionError) else: form =
UserProfileForm() return render(request, 'authenticate\\bolo.html',
context={'form': form})
@login_requireddef profile_page(request): # Fetching data from DB to show
user's complete profile page data = get_object_or_404(UserProfile,
user=request.user) data2 = get_object_or_404(User, user=request.user)
context = {'data': data, 'data2': data2} return render(request,
'authenticate\\profile.html', locals())
To test it further, I realized that the three fields (email, first_name, and
last_name) is coming from the "user". So, in the "userprofileview" I added
"user" for those fields in the below format.
pr.user.first_name = form.cleaned_data['first_name']
pr.user.last_name = form.cleaned_data['last_name'] pr.user.email =
form.cleaned_data['email']
However, I started to get below error
-------------------------------------------------Internal Server Error:
/fetch_data/ # HTML Template
Traceback (most recent call last):
File "C:\Python38\lib\site-packages\django\core\handlers\exception.py", line
34, in inner
response = get_response(request)
File "C:\Python38\lib\site-packages\django\core\handlers\base.py", line 115,
in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python38\lib\site-packages\django\core\handlers\base.py", line 113,
in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\anshu\djago-project\AUTHENTICATION\views.py", line 65, in
userprofileview
pr.user.first_name = form.cleaned_data['first_name']
KeyError: 'first_name'
--------------------------------------------------
It would be very kind of anybody who can help me rectify the issue. I have been
struggling for 4 days minimum now.
Amitesh
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/710931514.1164631.1588692639153%40mail.yahoo.com.