Issue
I get this error when I use:
@login_required('/login_backend/')
in my views.
I am using Django 1.5. My user model is:
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'
How to solve this?
Edit: My view code:
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:
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'
Solution
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/')
Answered By - koniiiik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.