[英]Django: 'User' object has no attribute 'user'
My website has a system of alerts. 我的网站有一个警报系统。 When a certain action occurs the system loggs an alert in the following model:
发生某种操作时,系统会在以下模型中记录警报:
class Alert(models.Model):
title = models.CharField(max_length=60)
read = models.BooleanField #if this is a new alert of not
for_user = models.ForeignKey(User) #which user will see it
link = models.CharField(max_length=100)
Many functions need to check the number of alerts a user has (Mostly to display the number of new alerts next to the alerts tab of the site). 许多功能需要检查用户的警报数量(主要是显示站点警报选项卡旁边的新警报数)。 Because of that, I created this function:
因此,我创建了这个函数:
@login_required()
def get_alertnum(user):
alert_objects = Alert.objects.filter(read = False, for_user=user)
num = 0
for n in alert_objects:
num += 1
return num
Which is accessed by this function: 这个功能可以访问:
@login_required()
def posting_draft(request):
user = request.user
user_drafts = Draft.objects.filter(user = user)
drafts = dict()
for d in user_drafts:
drafts[d.title] = d.id
alertnum = get_alertnum(user)
return render_to_response('posting_draft.html', {'STATIC_URL':STATIC_URL, 'draft_l' : drafts, 'selected':"dr", alertnum: alertnum})
But I get the following error: 但是我收到以下错误:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/posting/drafts
Django Version: 1.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'Knights',
'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/contrib/auth/decorators.py" in _wrapped_view
20. return view_func(request, *args, **kwargs)
File "/Users/Mike/Desktop/Main/Django-Development/BBN/Knights/views.py" in posting_draft
245. alertnum = get_alertnum(user)
File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/contrib/auth/decorators.py" in _wrapped_view
19. if test_func(request.user):
File "/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg/django/utils/functional.py" in inner
185. return func(self._wrapped, *args)
Exception Type: AttributeError at /posting/drafts
Exception Value: 'User' object has no attribute 'user'
You need to remove @login_required() decorator from the get_alertnum() function. 您需要从get_alertnum()函数中删除@login_required()装饰器。 The decorator assumes the first argument is a request object and is trying to access the user attribute.
装饰器假设第一个参数是请求对象,并且正在尝试访问用户属性。
Also you could simplify and speed up the function by: 您还可以通过以下方式简化和加速功能:
def get_alertnum(user):
return Alert.objects.filter(read=False, for_user=user).count()
Below is an explanation of the count method. 以下是计数方法的说明。
https://docs.djangoproject.com/en/dev/ref/models/querysets/#count https://docs.djangoproject.com/en/dev/ref/models/querysets/#count
This error also pops up when you use @login_required
decorator on class. 在类上使用
@login_required
装饰器时,也会弹出此错误。 For class decoration use @method_decorator(login_required)
. 对于类装饰,请使用
@method_decorator(login_required)
。 See more in the Class based views docs. 在基于类的视图文档中查看更多内容。
The @login_required
decorator only works on functions whose first argument is a request. @login_required
装饰器仅适用于第一个参数是请求的函数。 Your stack trace is because it tries to use the user
object as if it were a request
object, and it doesn't work. 您的堆栈跟踪是因为它尝试将
user
对象用作request
对象,并且它不起作用。 (as the other answer points out, user
objects don't have a .user
attribute) (正如其他答案所指出的,
user
对象没有.user
属性)
Perhaps instead get_alertnum()
could check user.is_authenticated()
first, and return 0
if the user is not authenticated. 也许相反,
get_alertnum()
可以先检查user.is_authenticated()
,如果用户未经过身份验证,则返回0
。
For example: 例如:
def get_alertnum(user):
if not user.is_authenticated():
return 0
else:
return Alerts.objects.filter(read=False, for_user=user).count()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.