0

我在使用时收到此错误: @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'
4

3 回答 3

2

The issue seems to be incorrect use of the login_required decorator. According to the documentation, the decorator takes two optional keyword arguments, redirect_field_name and login_url. Emphasis on the expression keyword arguments.

I'm not entirely sure what it is you want to achieve with the '/login_backend/' argument to the decorator, but if you pass it directly as the first argument, the decorator takes this as the function you want to decorate and bad stuff happens.

To fix (and this is only a guess on my part), try

@login_required(login_url='/login_backend/')
于 2013-02-12T10:18:30.710 回答
0

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

LOGIN_URL='/login_backend/'

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

@login_required
def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/login_backend/')
于 2020-08-16T18:38:29.497 回答
0
from django.utils.decorators import method_decorator


@method_decorator(login_required(login_url='/login/'))
def startPage(request):
    ....
    ....

这可能有助于干杯。

于 2015-12-14T09:51:30.530 回答