[Django]-Django User's DetailVew affects authenticated User

5👍

Actually, the user is not logged in, but an issue with your code. When you are sending the User data to template with context parameter user, it gets mixed up with request.user. And interestingly, if you try with that user (which is sent from DetailView), it will show is_authenticated true, so you would see the logged in user in based html. For example:

user = User.objects.first()
user.is_authenticated()  # It will return True

To prevent this, you need to send a different context parameter for DetailView. Like:

class ProfileView(DetailView):
    context_object_name = "profile_user"
    ...

and in template, use profile_user. Also, if you want only the logged in user to access that particular View, then you should subclass it from LoginRequiredMixin. For example:

from django.contrib.auth.mixins import LoginRequiredMixin

class ProfileView(LoginRequiredMixin, DetailView):
   ...
👤ruddra

Leave a comment