3

我的网站有一个警报系统。当发生特定操作时,系统会在以下模型中记录警报:

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)

许多功能需要检查用户拥有的警报数量(主要是在站点的警报选项卡旁边显示新警报的数量)。因此,我创建了这个函数:

@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

此函数访问的内容:

@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})

但我收到以下错误:

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'
4

3 回答 3

15

您需要从 get_alertnum() 函数中删除 @login_required() 装饰器。装饰器假定第一个参数是一个请求对象并试图访问用户属性。

您还可以通过以下方式简化和加速该功能:

def get_alertnum(user):
    return Alert.objects.filter(read=False, for_user=user).count()

下面是count方法的解释。

https://docs.djangoproject.com/en/dev/ref/models/querysets/#count

于 2012-08-09T19:59:58.310 回答
12

@login_required当您在课堂上使用装饰器时也会弹出此错误。供班级装饰使用@method_decorator(login_required)在基于类的视图文档中查看更多信息。

于 2016-02-18T12:01:14.140 回答
2

@login_required装饰器仅适用于第一个参数是请求的函数。您的堆栈跟踪是因为它尝试像使用user对象一样使用该对象request,但它不起作用。 (正如另一个答案指出的那样,user对象没有.user属性)

也许get_alertnum()可以先检查user.is_authenticated()0如果用户未通过身份验证则返回。

例如:

def get_alertnum(user):
    if not user.is_authenticated():
        return 0
    else:
        return Alerts.objects.filter(read=False, for_user=user).count()
于 2012-08-09T20:04:41.580 回答