Ok. Regards, Chetan Ganji +91-900-483-4183 [email protected] http://ryucoder.in
On Wed, May 6, 2020 at 11:42 AM 'Amitesh Sahay' via Django users < [email protected]> wrote: > Hello Chetan, > > I got the issue resolved. Below are the correct views: > > 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() > pr.user = User.objects.get(id=request.user.id) > 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) > data2 = User.objects.get(id = request.user.id) > context = {'data': data, 'data2': data2} > return render(request, 'authenticate\\profile.html', locals()) > > > > Regards, > Amitesh > > > On Wednesday, 6 May, 2020, 10:34:05 am IST, 'Amitesh Sahay' via Django > users <[email protected]> wrote: > > > Hello Chetan, > > Below is how I have created the forms. > > from django.contrib.auth.forms import UserCreationForm > from django.contrib.auth.models import User > from .models import UserProfile > from django import forms > > > class SignUpForm(UserCreationForm): > email = forms.EmailField() > first_name = forms.CharField(max_length=100) > last_name = forms.CharField(max_length=100) > > class Meta: > model = User > fields = ('username', 'first_name', 'last_name', 'email', > 'password1', 'password2') > > > class UserProfileForm(forms.ModelForm): > Photo = forms.FileField( max_length=100) > #widget=forms.ClearableFileInput(attrs={'multiple': True}), > dob = forms.DateField(widget=forms.TextInput(attrs={'type': 'date'})) > country = forms.CharField(max_length=100) > State = forms.CharField(max_length=100) > District = forms.CharField(max_length=100) > phone = forms.CharField(max_length=10) > > class Meta: > model = UserProfile > fields = ('Photo', 'dob', 'country', 'State', 'District', 'phone') > > > I hope that helps. Please let me know. Just to let you know that there was > a time during the development when I could only see the User model data on > my page, but none from the UserProfile model. During the debug process I > have made changes in both the files over the last couple of weeks, so now I > have reached to the point where I can see only the UserProfile data. > > Just wanted to give you some insight. > > Regards, > Amitesh > > > On Wednesday, 6 May, 2020, 12:11:32 am IST, 'Amitesh Sahay' via Django > users <[email protected]> wrote: > > > Hi Chetan, > > The default user model already has those three fields, right? And since I > have extended the User as a onetoone field inside the UserProfile model, > so shouldn't that work? I mean, that's my understanding. May be I am wrong. > Let me know, just for the sake of clarity. > > Right now I don't have access to my system. I will send the code snippet > of the forms.py, may be then you can give more inputs > > Thank you so much for your time though > > Amitesh > > Sent from Yahoo Mail on Android > <https://go.onelink.me/107872968?pid=InProduct&c=Global_Internal_YGrowth_AndroidEmailSig__AndroidUsers&af_wl=ym&af_sub1=Internal&af_sub2=Global_YGrowth&af_sub3=EmailSignature> > > On Tue, 5 May 2020 at 23:32, Chetan Ganji > <[email protected]> wrote: > Hi Amitesh, > > Assuming you are using model forms in django without any customisation, > as UserProfile model does not have first_name, last_name and email field, > reading the first_name from cleaned_data is failing. > > To solve it, you have to add 3 extra fields in the UserProfileForm > i.e. first_name, last_name and email, and pop them before saving the form. > also save these three fields on the user model. > > *request.user.first_name = form.cleaned_data.pop('first_name')* > request.user.save() > > form.save() > > Cheers! > > > Regards, > Chetan Ganji > +91-900-483-4183 > [email protected] > http://ryucoder.in > > > > On Tue, May 5, 2020 at 10:32 PM 'Amitesh Sahay' via Django users < > [email protected]> wrote: > > Hello Chetan, > > I was doing some random test, so I put "User" there. It is not the part of > the original code. > > Below is models.py > > *from django.db import models* > *from django.urls import reverse* > *from django.contrib.auth.models import User* > > > *class UserProfile(models.Model):* > * user = models.OneToOneField(User, on_delete=models.CASCADE)* > * Photo = models.FileField(upload_to='documents/%Y/%m/%d)* > * uploaded_at = models.DateTimeField(auto_now_add=True)* > * dob = models.DateField(max_length=20)* > * country = models.CharField(max_length=100)* > * State = models.CharField(max_length=100)* > * District = models.CharField(max_length=100)* > * phone = models.CharField(max_length=10)* > > * def get_absolute_url(self):* > * return reverse('profile', kwargs={'id': self.id > <http://self.id>})* > > Kindly give me the modified code that you think would work. > > Regards, > Amitesh > > > On Tuesday, 5 May, 2020, 09:24:58 pm IST, Chetan Ganji < > [email protected]> wrote: > > > 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 > [email protected] > http://ryucoder.in > > > On Tue, May 5, 2020 at 9:02 PM 'Amitesh Sahay' via Django users < > [email protected]> 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 [email protected]. > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CAMKMUjvsJQ-7Zyh4t4x82pfA1KJYKbu_PtpSw%2BqiG7x38JDF4g%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAMKMUjvsJQ-7Zyh4t4x82pfA1KJYKbu_PtpSw%2BqiG7x38JDF4g%40mail.gmail.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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/2041909068.1176186.1588698102511%40mail.yahoo.com > <https://groups.google.com/d/msgid/django-users/2041909068.1176186.1588698102511%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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CAMKMUjvpcP%2BNsduU4ryeGaatUyU%3D-OPM1R6Wt6WMZz_rBrVWKA%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAMKMUjvpcP%2BNsduU4ryeGaatUyU%3D-OPM1R6Wt6WMZz_rBrVWKA%40mail.gmail.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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/1441521709.1186686.1588704038060%40mail.yahoo.com > <https://groups.google.com/d/msgid/django-users/1441521709.1186686.1588704038060%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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/2015737819.1325046.1588741408704%40mail.yahoo.com > <https://groups.google.com/d/msgid/django-users/2015737819.1325046.1588741408704%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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/168480630.1356055.1588745545917%40mail.yahoo.com > <https://groups.google.com/d/msgid/django-users/168480630.1356055.1588745545917%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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMKMUjuBmPC9ydC2DdaVn-HyvWAjObXu%2BwJ0KNNewkftnpudNA%40mail.gmail.com.

