I am trying to show a public user profile when someone goes to something like http://localhost:8000/@john - It seems user = get_object_or_404(User, username=username)
is authenticating a user ?
# http://localhost:8000/@john
def PublicProfile(request, username):
user = get_object_or_404(User, username=username)
# print("FirstName = ", user.first_name)
print(user.is_authenticated) # This is True even when not loggedin !
return render(request, 'public_profile.html', { 'user': user })
Nothing is actually being authenticated.
See the docs for is_authenticated
to understand what’s happening.
How do I get a user object based on username that is not loggedin ?
It seems like all the examples are based on an already logged-in user.
I just want to display a public profile page based on a username.
Or I want to set is_authenticated to False in the my views.py for def PublicProfile(request, username):
user = get_object_or_404(User, username=username)
# print("FirstName = ", user.first_name)
user.is_authenticated = False
I don’t understand what you’re asking for here. If you want the profile, access the profile. I’m not seeing where the is_authenticated function has anything to do with accessing a profile.
How else do I get a user details (like full name etc) from a given username string ?
This does not require anyone to login to the system. No authentication.
Query the User table using username as the filter.
Isn’t that user = get_object_or_404(User, username=username)
?
The only other alternative I could find is this :
User = get_user_model()
user = User.objects.filter(username=username)
print(user[0])
But even user[0].is_authenticated
is True which I don’t want.
User. is_authenticated has nothing to do with if the user is logged in or not. Is that what you’re actually after finding?
Did you read the docs on what is_authenticated actually does?
Yes, given a username, I should be able to get the user details like first name and last name.
But I don’t want the property is_authenticated
to be True.
This sound a lot like an X-Y Problem.
By definition, is_authenticated is True for all entries in the User table.
Why do you want it to be otherwise? What are you actually trying to accomplish by this? (It sounds to me like you’re assuming is_authenticated
has a different meaning than what it has, and you’re trying to use it for something else.)
I that case I should change my login / logout toggle code which is this in the navbar :
<ul class="navbar-nav me-auto mb-2 mb-lg-0 ">
{% if user.is_authenticated %}
<li class="nav-item">
<a class="btn btn-login" href="{% url 'dashboard' %}">Dashboard</a>
</li>
<li class="nav-item">
<a class="btn btn-login" href="{% url 'logout' %}">Logout</a>
</li>
{% else %}
<li class="nav-item">
<a href="{% url 'login' %}" class="btn btn-login">Login</a>
</li>
{% endif %}
{% if user.is_authenticated == False %}
<li class="nav-item">
<a class="btn btn-signup" href="{% url 'signup' %}">Sign up</a>
</li>
{% endif %}
</ul>
Not if the user
object referenced in the template is retrieved from request.user
- in which case this is precisely how it’s intended to be used.
In that case I have to use a different variable name for public user details.
def PublicProfile(request, username):
user = get_object_or_404(User, username=username)
# print("FirstName = ", user.first_name)
user_profile = { 'full_name': user.first_name + ' ' + user.last_name, 'username': username }
return render(request, 'public_profile.html', { 'user_profile': user_profile })
That’s probably best anyway, to prevent conflict with the context middleware.