2013-02-12 74 views
0

我在使用時出現此錯誤: @login_required('/login_backend/')在我的意見中。'函數'對象在Django中沒有屬性'用戶'錯誤?

我正在使用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' 
+0

請發佈您的視圖代碼(以及login_required裝飾器),這很可能發生錯誤的地方;否則無法幫助你。 – koniiiik 2013-02-12 10:07:28

+0

查看我更新的問題。 – pynovice 2013-02-12 10:09:54

+0

思考它,回溯也會有所幫助...... – koniiiik 2013-02-12 10:11:22

回答

2

這個問題似乎是不正確的使用login_required裝飾的。根據the documentation,裝飾器需要兩個可選的關鍵字參數,redirect_field_namelogin_url。重點表達關鍵字參數

我不完全確定你想用decorator的參數'/login_backend/'來達到什麼目的,但是如果你直接把它作爲第一個參數傳遞,那麼裝飾器將把它作爲你想裝飾的函數和壞東西發生。

要解決(這只是一種猜測對我而言),嘗試

@login_required(login_url='/login_backend/') 
+0

是的。這就是我所錯過的。謝謝man.I爲你另一個:http://stackoverflow.com/questions/14830260/not-able-to-authenticate-with-correct-username-and-password-in-django – pynovice 2013-02-12 10:36:05

0
from django.utils.decorators import method_decorator 


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

這可能有助於 乾杯。

相關問題