2017-09-18 88 views
3

我在我的django應用程序中使用社交登錄。所以,我在settings.py文件中添加了額外的後端。django'AnonymousUser'對象沒有屬性'_meta'

AUTHENTICATION_BACKENDS = [ 
    'django.contrib.auth.backends.ModelBackend', 
    'social_core.backends.open_id.OpenIdAuth', 
    'social_core.backends.google.GoogleOpenId', 
    'social_core.backends.google.GoogleOAuth2', 
    'social_core.backends.google.GoogleOAuth', 
    'social_core.backends.twitter.TwitterOAuth', 
    'social_core.backends.facebook.FacebookOAuth2', 
    'social_core.backends.github.GithubOAuth2', 

]

我還用於註冊UserCreationForm,

class SignupForm(UserCreationForm): 
    first_name = forms.CharField(max_length=30, required=True, help_text='Required.') 
    last_name = forms.CharField(max_length=30, required=True, help_text='Required.') 
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.') 

    class Meta: 
      model = User 
      fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2') 

這是意見文件,

def signup(request): 
    if request.method == 'POST': 
      form = SignupForm(request.POST) 
      if form.is_valid(): 
        form.save() 
        username = form.cleaned_data.get('username') 
        raw_pass = form.cleaned_data.get('password') 
        user = authenticate(request, username=username,password=raw_pass) 
        login(request,user,backend='django.contrib.auth.backends.ModelBackend') 
        url = reverse('location:get_location') 
        print("location_url ", url) 
        return HttpResponseRedirect(url) 
    else: 
      form = SignupForm() 
    return render(request, 'signup.html', {'form':form}) 

現在,我當我點擊註冊得到這個錯誤我的表單上的按鈕,

'AnonymousUser' object has no attribute '_meta' 

在該行,

login(request,user,backend='django.contrib.auth.backends.ModelBackend') 

爲什麼會這樣?

我可以在我的管理面板中看到該用戶已保存。

什麼是造成此錯誤?以及如何解決它?

編輯 -

Internal Server Error: /signup/ 
Traceback (most recent call last): 
    File "/home/luvpreet/Envs/weather/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner 
    response = get_response(request) 
    File "/home/luvpreet/Envs/weather/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response 
    response = self.process_exception_by_middleware(e, request) 
    File "/home/luvpreet/Envs/weather/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response 
    response = wrapped_callback(request, *callback_args, **callback_kwargs) 
    File "/home/luvpreet/Desktop/drf-vogo/weather/weather/pilot/views.py", line 45, in signup 
    login(request,user,backend='django.contrib.auth.backends.ModelBackend') 
    File "/home/luvpreet/Envs/weather/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 154, in login 
    request.session[SESSION_KEY] = user._meta.pk.value_to_string(user) 
    File "/home/luvpreet/Envs/weather/local/lib/python2.7/site-packages/django/utils/functional.py", line 239, in inner 
    return func(self._wrapped, *args) 
AttributeError: 'AnonymousUser' object has no attribute '_meta' 
+0

@Alasdair請檢查stacktrace,'from django.contrib.auth導入登錄,驗證 ' – Luv33preet

回答

7

您已經擁有了用戶,當您保存表單,這樣你就不會需要調用authenticate因爲你調用login()時已經提供了後端:

user = form.save() 
login(request, user, backend='django.contrib.auth.backends.ModelBackend') 
0

來到這裏尋找這個錯誤。我們的堆棧是django-oscar + w。。原來我們從AUTHENTICATION_BACKENDS中刪除了oscar.apps.customer.auth_backends.EmailBackend。把它回來解決了這個問題。

相關問題