I am using social login in my Django app. So, I have added additional backends in my settings.py
file.
我在我的Django应用程序中使用社交登录。所以,我在settings.py文件中添加了额外的后端。
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',
]
I have also used UserCreationForm
for signup,
我也使用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' )
This is the views file,
这是视图文件,
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})
Now, I get this error when i click signup button on my form,
现在,当我单击表单上的注册按钮时出现此错误,
'AnonymousUser' object has no attribute '_meta'
at the line,
在线,
login(request,user,backend='django.contrib.auth.backends.ModelBackend')
Why so ?
为什么这样 ?
I can see in my admin panel that user has been saved.
我可以在我的管理面板中看到用户已保存。
What is causing this error ? and how to solve it ?
导致此错误的原因是什么?以及如何解决?
EDIT -
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'
2 个解决方案
#1
11
You already have the user when you save the form, so you don't need to call authenticate
since you already provide the backend when calling login()
:
保存表单时已有用户,因此在调用login()时已经提供了后端,因此无需调用authenticate:
user = form.save()
login(request, user, backend='django.contrib.auth.backends.ModelBackend')
#2
0
Came here looking for this error. Our stack is django-oscar + wagtail. It turns out we removed oscar.apps.customer.auth_backends.EmailBackend
from our AUTHENTICATION_BACKENDS
. Putting it back solved the issue.
来这里寻找这个错误。我们的堆栈是django-oscar + wagtail。事实证明我们从AUTHENTICATION_BACKENDS中删除了oscar.apps.customer.auth_backends.EmailBackend。把它放回来解决了这个问题。
#1
11
You already have the user when you save the form, so you don't need to call authenticate
since you already provide the backend when calling login()
:
保存表单时已有用户,因此在调用login()时已经提供了后端,因此无需调用authenticate:
user = form.save()
login(request, user, backend='django.contrib.auth.backends.ModelBackend')
#2
0
Came here looking for this error. Our stack is django-oscar + wagtail. It turns out we removed oscar.apps.customer.auth_backends.EmailBackend
from our AUTHENTICATION_BACKENDS
. Putting it back solved the issue.
来这里寻找这个错误。我们的堆栈是django-oscar + wagtail。事实证明我们从AUTHENTICATION_BACKENDS中删除了oscar.apps.customer.auth_backends.EmailBackend。把它放回来解决了这个问题。