[英]How do I get the first name and last name of a logged in user in Django?
I need to get the first name and last name of a user for a function in my views How do I do that? 我需要在我的视图中获取函数用户的名字和姓氏我该怎么做? What is the syntax for it?
它的语法是什么? I used request.user.email for the email, but I don't know see an option for a first name or last name.
我使用request.user.email作为电子邮件,但我不知道看到名字或姓氏的选项。 How do I go about doing this?
我该怎么做呢?
Should I import the model into the views and then use it? 我应该将模型导入视图然后使用它吗?
There are multiple ways to get the information you require. 有多种方法可以获取所需的信息。
request.user.get_full_name()
, returns the first name and last name, separated by a space. request.user.get_full_name()
,返回名字和姓氏,用空格分隔。
request.user.get_short_name()
, returns just the first name request.user.get_short_name()
,只返回第一个名称
You can also access the attributes directly, with request.user.first_name
and request.user.last_name
, but it is preferred to use the methods as the attributes may change in future versions. 您还可以使用
request.user.first_name
和request.user.last_name
直接访问属性,但最好使用这些方法,因为在将来的版本中属性可能会更改。
The django user model has a rich set of methods, which you can read about in the reference for the auth
application . django用户模型有一组丰富的方法,您可以在
auth
应用程序的参考中阅读这些方法。
您需要first_name
和last_name
属性: request.user.first_name
和request.user.last_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.