django get_profile错误

时间:2012-07-17 13:04:38

标签: django django-models django-templates django-views

我希望在下面的settings.py

中包含的视图中获取我的userprofile 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'

1 个答案:

答案 0 :(得分:2)

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

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

@login_required
def foo(request):
    profile_id = request.user.get_profile().id