Priyank Kodesia
Priyank Kodesia

Reputation: 61

Unable to retrieve logged in user specific content on html page in django

I have successfully created a login page and i am able to login the user after signing them up. But i fail to retrieve the user data and display it in user's home page apart from user's username and email(which come up with inbuilt User model since i have inherited it.). Here i want to show the profile picture of the user on its home page after logging in.Thanks in advance.

models.py

class Profile(models.Model):

user = models.OneToOneField(User)
first_Name=models.CharField(max_length=26,default=" ")
last_Name=models.CharField(max_length=26,default=" ")
gender=models.CharField(max_length=10)
father_name=models.CharField(max_length=56)
profile_pic=models.ImageField(max_length=100,upload_to='profile_pics')


@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
   if created:
      profile=Profile.objects.create(user=instance)
      instance.profile.save()

def __str__(self):
    return self.user.username

forms.py

class UserForm(forms.ModelForm): password=forms.CharField(widget=forms.PasswordInput())

class Meta():
    model = User
    fields=('username','password')

class UserProfileInfoForm(forms.ModelForm):

class Meta():
    model=Profile
    fields=('first_Name','last_Name','gender','father_name','profile_pic')

views.py

@login_required
def IndexView(request,user_id):
user = User.objects.get(pk=user_id)
profile_pict=user.profile.profile_pic
return render(request,"studenthome.html",{
    'username': user.username,
    'address':user.profile.address,
    'profile_pict':profile_pict,

})



class LoginView(View):
form_class = UserForm
template_name="login.html"

def get(self,request):
    form=self.form_class(None)
    return render(request,self.template_name,{'form':form})

def post(self, request):
    password = request.POST['password']
    username = request.POST['username']
    user = authenticate(username=username, password=password)

    if user is not None:
        if user.is_active:
            if user.is_staff:
               login(request, user)
               return render(request,'teacher.html',{'username':user.username})
            else:
               login(request,user)
               return render(request,'studenthome.html',{'username':user.username})
            #    return redirect('IITMApp:index')

        else:
            return HttpResponse("Inactive user.")
    else:
        # return redirect("IITApp:login")
        return render(request, 'login.html',{
        'login_message' : 'Enter the username and password correctly',})

    return render(request, "studenthome.html")

Upvotes: 0

Views: 114

Answers (1)

Vishal Mopari
Vishal Mopari

Reputation: 607

By looking at your code you are only passing username to your template instead if complete user object. See line:

return render(request,'teacher.html',{'username':user.username})

if instead if this you pass it as:

return render(request,'teacher.html',{'user':user})

also you can use django's default login function as given here.

you will be able to access all the fields of user model in your template.

Let me know if this doesn't work.

Upvotes: 1

Related Questions