开发者

Want to split user name in django-nonrel

开发者 https://www.devze.com 2023-02-17 02:49 出处:网络
In my django-nonrel project,all usernames are joined with \'_\'(underscore). For example, if username is \'guest_test\' then I want to split guest and test seperate.I am trying with the following code

In my django-nonrel project,all usernames are joined with '_'(underscore). For example, if username is 'guest_test' then I want to split guest and test seperate.I am trying with the following code:

CurrentUser=request.user
myuser=CurrentUser.split('_')
username=myuser.pop(0)
institute=myuser.pop(0)
print username

But its giv开发者_运维百科ing error as:

'User' object has no attribute 'split'.

How to do it?


You're confusing the User object which has lots of attributes with the username which is one of those attributes.

CurrentUser contains a User object, so you need to get the username of this User to then do the split on. Try:

institute,username = CurrentUser.username.split('_')


You are trying to do a split function on the user object which is meant only for string object. You can do a type cast like this .

CurrentUser=str(request.user)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号