'function'对象在Django中没有属性'user'错误?

时间:2013-02-12 09:57:54

标签: django django-authentication

我使用时收到此错误: 我的观点中@login_required('/login_backend/')

我正在使用Django 1.5。我的用户模型是:

class User(AbstractBaseUser):
    #id = models.IntegerField(primary_key=True)
    #identifier = models.CharField(max_length=40, unique=True, db_index=True)
    username = models.CharField(max_length=90, unique=True, db_index=True)
    create_time = models.DateTimeField(null=True, blank=True)
    update_time = models.DateTimeField(null=True, blank=True)
    email = models.CharField(max_length=225)
    #password = models.CharField(max_length=120)
    external = models.IntegerField(null=True, blank=True)
    deleted = models.IntegerField(null=True, blank=True)
    purged = models.IntegerField(null=True, blank=True)
    form_values_id = models.IntegerField(null=True, blank=True)
    disk_usage = models.DecimalField(null=True, max_digits=16, decimal_places=0, blank=True)
    #last_login = models.DateTimeField()

    objects = UserManager()
    USERNAME_FIELD = 'email'
    class Meta:
        db_table = u'galaxy_user'

如何解决这个问题?

修改:我的观看代码:

def login_backend(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user)
            return HttpResponseRedirect('/overview/')
        else:
            return HttpResponseRedirect('/login_backend/')
    else:
        return render_to_response('login_backend.html', context_instance=RequestContext(request))

@login_required('/login_backend/')
def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/login_backend/')

@login_required('/login_backend/')
def show_files(request):
    try:
        log_id = request.user.id
        username = request.user.username
        b = File.objects.filter(users_id=log_id, flag='F', flag_r='S')  # Get the user id from session .delete() to use delete
        total_files = File.objects.filter(users_id=log_id, flag='F').count()
        total_size = File.objects.filter(users_id=log_id, flag='F').aggregate(Sum('size_overview'))
        total_size = total_size['size_overview__sum']
        total_size = size(total_size, system=alternative)
        return render_to_response('overview.html', {'overview_files': b, 'total_files':total_files, 'total_size':total_size, 'username': username}, context_instance=RequestContext(request))
    except TypeError:
        return render_to_response('overview.html', {'overview_files': b, 'total_files':total_files, 'total_size':total_size, 'username': username}, context_instance=RequestContext(request))

回溯:

Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  103.                     resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in resolve
  321.                     sub_match = pattern.resolve(new_path)
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in resolve
  223.             return ResolverMatch(self.callback, args, kwargs, self.name)
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in callback
  230.         self._callback = get_callable(self._callback_str)
File "/usr/local/lib/python2.6/dist-packages/django/utils/functional.py" in wrapper
  29.         result = func(*args)
File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py" in get_callable
  97.             mod = import_module(mod_name)
File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/zurelsoft/workspace/genalytics/fileupload/backend.py" in <module>
  37. @login_required('/login_backend/')
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
  24.             if test_func(request.user):

Exception Type: AttributeError at /login_backend/
Exception Value: 'function' object has no attribute 'user'

3 个答案:

答案 0 :(得分:2)

问题似乎是错误地使用了login_required装饰器。根据{{​​3}},装饰器采用两个可选的关键字参数redirect_field_namelogin_url。强调表达式关键字参数

我不完全确定你想用装饰器的'/login_backend/'参数来实现它是什么,但是如果你直接将它作为第一个参数传递,装饰器将它作为你要装饰的函数发生了坏事。

要修复(这只是我的猜测),试试

@login_required(login_url='/login_backend/')

答案 1 :(得分:0)

Sub ADO_conn_Click()
Dim conn As New Connection
Dim rs As New Recordset

    strcon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=test1.server.net:63306" & _
    "User Id=admin;Password=test"

    conn.Open (strcon)

    qry = "SELECT * FROM Employee"
    rs.Open qry, conn, adOpenKeyset

    rs.Close
    conn.Close
End Sub

这可能会有所帮助 欢呼声。

答案 2 :(得分:0)

您可以尝试的另一件事是将以下行添加到设置的底部。py

LOGIN_URL='/login_backend/'

这应该允许您使用装饰器,而不必像下面的示例那样指定登录参数。

@login_required
def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/login_backend/')