As title says, what is the difference between request.user and request.user.username? I use them to compare them with some variables and sometimes it works using the first and sometimes using the second
As title says, what is the difference between request.user and request.user.username? I use them
Share
I think that questions comes only after you were using user objects in your templates like
{{user}}and{{user.username}}and understand that templates rendering the same.The point is that when you render the template, django automatically converts variables to its unicode representation (strings), and in your view function it can be not a string but an instance of some model.
User class from app django.contrib.auth has a unicode representation like
And maybe that’s why you were confusing
useranduser.username.This misunderstanding can be dangerous, cause when you are typing something like
you won’t get
TypeError, cause any django model inhereted from models.Model has a operatorwhich finds if user1 and user2.username had the same class.
Anyway, I think after practicing a little you’ll find difference between string and objects of some model.