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