简体   繁体   English

如何在Django中获取登录用户的名字和姓氏?

[英]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_namerequest.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_namelast_name属性: request.user.first_namerequest.user.last_name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django,如何使用名和姓对用户进行身份验证? - Django, How authenticate user with first name and last name? 如何查看 Django 中用户的名字? - How do I view the first name of a user in Django? 如何在表单 django 中添加登录的用户名 - How can I add the logged in user's name in form django 如何利用Django用户模型的内置名字和姓氏功能? - How can I utilise the built in first and last name features of the Django User Model? 如何使用姓氏和名字对列表进行排序 - How do i sort a list with first and last name by the last names 当用户写名字和姓氏框时,我希望它使用 tkinter 和 python-docx 在单元格中打印它,我该怎么做? - When the user write the first name and the last name boxs I want it to get print it in cell using tkinter and python-docx, how can I do that? 如何更新 Django 模型用户的名字、姓氏和电子邮件并更新单独模型中的其他字段 - How can I update first_name, last_name & email of Django model User and update other field in separate models 如何获取登录用户的名称(Flask,SQLAlchemy) - How to get name of logged user (Flask, SQLAlchemy) 如何返回用户ID而不是first_name和last_name-Django 1.6 - How to return user id instead of first_name and last_name - Django 1.6 如何返回与url django rest相同的名字或姓氏的所有用户实例? - How to return all user instances with same first name or last name as in url django rest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM