Hi Amitesh,

If you post the models, then only someone will be able to give you exact
solution.

Couple of things I noticed.
How is this even working??
You have not defined User variable in the function??
If its the default User model, how would passing it to UserProfile will
help in your scenario??
pr = UserProfile(User)

You dont need this line as it is already available as request.user.  Why do
you want to fetch it again, when it is already available???
*pr.user = User.objects.get(id=request.user.id <http://request.user.id/>)*
data2 = get_object_or_404(User, user=request.user)

In the *profile_page *view, you can use reverse relation on the user model.

Regards,
Chetan Ganji
+91-900-483-4183
ganji.che...@gmail.com
http://ryucoder.in


On Tue, May 5, 2020 at 9:02 PM 'Amitesh Sahay' via Django users <
django-users@googlegroups.com> wrote:

> 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
> <http://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_required*
> *def 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 django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/710931514.1164631.1588692639153%40mail.yahoo.com
> <https://groups.google.com/d/msgid/django-users/710931514.1164631.1588692639153%40mail.yahoo.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMKMUjvsJQ-7Zyh4t4x82pfA1KJYKbu_PtpSw%2BqiG7x38JDF4g%40mail.gmail.com.

Reply via email to