Hi so i have my views below, in which it takes you to the profile page of the user.
what i basically want is that the request.user be equal to the profile name, it should print out the profile belongs to the Said User in the Terminal. But it does not do that. Please see my logic below.
def user_profile(request,user_name):
users = get_list_or_404(User,username = user_name)
print(request.user,"\n")
print(user_name,"\n")
if request.user == user_name:
print(f"This is Your Profile {request.user}")
else:
print(f"This is not your Profile {request.user}")
context = {"users": users}
return render(request, "testing/user_profile.html", context)
Now below are the screenshots from the terminal as well as the Profile Page
Terminal
Profile Page
cehceh
January 17, 2023, 11:13am
2
You must know that (There is something not proper , the “IF” statement is right and always right ) you maybe have something wrong
try
request.user.username == user_name
instead of
request.user == user_name
I think
request.user
need an instance of your User model not a “str” value
hope this help you
thanks for the reply, but i again get error
'WSGIRequest' object has no attribute 'username'
cehceh
January 17, 2023, 12:22pm
4
If the above line works fine with you that’s right,
So it must the next lines work fine also
if request.user.username == user_name:
print(f"This is Your Profile {request.user}")
else:
print(f"This is not your Profile {request.user}")
Else try to show us your code and settings
1 Like
ok finally i figured it out.
the type of request.user and user_name were not the same.
so all i had to do was
if str(request.user) == user_name:
This might be working for you now, but it’s still “wrong”. You should be using the solution provided to you previously.
1 Like
but when i print request.user.username
it is blank in the terminal.
If you are using a custom User object, we’d need to see that.
1 Like
cehceh
January 17, 2023, 2:10pm
9
Try to add new user in your database and login with it
Then print again request.user.username
and request.user.id
and see what do you get.
1 Like
I never changed the default USER model.
But did install django-allauth
Will do that as soon when I get back to my home laptop.
As right now i only have one user in django and that is the superuser.
That’s an interesting aspect to this. (I don’t know enough about django-allauth to know whether that’s a factor in this situation or not.)
You could check your database table to see if the username
field of User
is being populated.
cehceh
January 17, 2023, 7:53pm
13
@firaki12345
OK, open your database (I don’t know what’s it? “but it’s the same”) on the terminal or if you have UI for it to check.
If you have a CustomUser model inherited from the “AbstractUser” you will find “username” as a field
Check your query
users = get_list_or_404(User,username = user_name)
Your database already have “username” field
By the way if you have only one user as you have said try to check by this query
qs = User.objects.values('id', 'username').first()
OR
qs = User.objects.values('id', 'username').last()
Then print the qs to check quickly
Using first()
HI,so i added a secnd user by signng them up.
and this is what i get in my mange.py shell
>>> User.objects.values('id','username')
<QuerySet [{'id': 1, 'username': 'Firaki'}, {'id': 2, 'username': 'Firoz'}]>
cehceh
January 18, 2023, 8:32am
15
That’s good now you can print print(request.user.username)
You will get the authenticated user on your terminal
Here user may be blank because the user is not authenticated
And you can use the original code
if request.user.username == user_name:
print(f"This is Your Profile {request.user}")
else:
print(f"This is not your Profile {request.user}")
1 Like
thanks a lot, yes you were , it showed blank because i was not logged in
and it retuns a string so no need for me to convert it into a string.
thanks