0

我想在我已经包含在 settings.py 下面的视图中获取我的用户配置文件 ID

 AUTH_PROFILE_MODULE = 'users.UserProfile'

查看代码:

def savecontent(request):
 try:
  logging.debug("=========================== in save content")
  logging.debug(dir(request.user))
  my_id = request.user.get_profile().id
  logging.debug(my_id)

错误是:

 2012-07-17 13:00:00,564 ERROR Error while saing content
 Traceback (most recent call last):
 File "/opt/labs/labs_site/content/views.py", line 21, in savecontent
 my_id = request.user.get_profile().id
 File "/opt/labs/django/django/utils/functional.py", line 185, in inner
 return func(self._wrapped, *args)
 AttributeError: 'AnonymousUser' object has no attribute 'get_profile'
4

1 回答 1

2
AttributeError: 'AnonymousUser' object has no attribute 'get_profile'

您必须有经过身份验证的用户才能检索个人资料,这是您的问题

@login_required
def foo(request):
    profile_id = request.user.get_profile().id
于 2012-07-17T13:11:46.073 回答